【2024.9.29练习】R 格式

题目描述


题目分析

带小数点的高精度乘法。小数点在计算时忽略,只需在最终打印字符串的时候在合适位置四舍五入即可。对于,可理解为对d乘2总共n次。因此使用"单精度×高精度"类型的算法足矣。


我的代码

一开始代码有错误,我只想到了对小数点前最后一位四舍五入,却没想到末位进一之后可能会满10,然后要再向前一位借1。后面又写了一个进位函数解决了问题。

并且一开始的最大数组长度写小了,考虑到还有的系数,应该再增加300位。测试用例果真说明如此。看来以后数组长度能开大还是尽量大一些。

cpp 复制代码
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int LEN = 2030; //数组最大长度
//预处理
void clear(int a[]) {
	for (int i = 0; i < LEN; ++i) a[i] = 0;
}
//是否有浮点
int p = 0; //默认的小数点在第几位之前
bool check_point(string s) {
    int len = s.length();
    for (int i = 0; i < len; i++) {
        if (s[i] == '.') {
            p = len - i - 1;
            return true;
        }
    }
    return false;
}
//读取字符串并转换为高精度数组
void read(int a[]) {
    string s;
    cin >> s;
	clear(a);
	int len = s.length();
    if (check_point(s)) {
        int j = 0;
        for (int i = 0; i < len; i++) {
            if (s[i] == '.') {
                j--;
            }
            else {
                a[len - j - 2] = s[i] - '0';
            }
            j++;
        }
    }
    else {
        for (int i = 0; i < len; i++) a[len - i - 1] = s[i] - '0';
    }
}
//高精度×单精度
void mul_short(int a[], int b, int c[]) {
    clear(c);

    for (int i = 0; i < LEN - 1; ++i) {
        c[i] += a[i] * b;
        if (c[i] >= 10) {
            c[i + 1] += c[i] / 10;
            c[i] %= 10;
        }
    }
}
//数组复制
void copyy(int a[],int b[]) { //a to b
    clear(b);
    for (int i = 0; i < LEN; i++) {
        b[i] = a[i];
    }
}
void print(int a[]) {
    int i;
    for (i = LEN - 1; i >= p+1; i--)
        if (a[i] != 0) break;
    for (; i >= p; --i) cout << a[i];
}
void print2(int a[]) {
    int i;
    for (i = LEN - 1; i >= 1; --i)
        if (a[i] != 0) break;
    for (; i >= 0; --i) putchar(a[i] + '0');
    putchar('\n');
}
void addone(int a[],int p) {
    a[p]++;
    if (a[p] >= 10) {
        a[p] %= 10;
        addone(a,p+1);
    }
}
int d[LEN];
int ans[LEN];
int main() {
    int n;
    cin >> n;
    read(d);
    //乘2共n次
    for (int i = 0; i < n; i++)
    {
        mul_short(d, 2, ans);
        copyy(ans, d);
    }
    //print2(ans);
    if (ans[p - 1] > 4) {
        addone(ans,p);
    }
    print(ans);
	return 0;
}

这道题不仅函数不完全是我自己写的(因为记不住),而且编写和纠错过程也十分漫长,常常被自己绕晕。高精度运算函数默写难度还是很大的,目前自己各方面的水平都还完全不行,要熟练掌握算法还需多加训练和思考!!

相关推荐
聚客AI18 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v20 小时前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工1 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农1 天前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了1 天前
AcWing学习——双指针算法
c++·算法
moonlifesudo1 天前
322:零钱兑换(三种方法)
算法
NAGNIP2 天前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队2 天前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法