蒟蒻求助(悬关)

P1009 [NOIP1998 普及组] 阶乘之和

Sky_Aurora @ 2024-07-18 16:04:31

WA代码:

#include<iostream>
#include<cstdio>
using namespace std;
string funa(string num)
{
    int a[10005], c[10005];
    int len = num.length();
    for (int i = len - 1;i >= 0;i--)
        a[len - i] = num[i] - '0';
    for (int i = 1;i <= len;i++)
        for (int j = 1;j <= len;j++)
            c[i + j - 1] += a[i] * a[j];
    len *= 2;
    for (int i = 1;i <= len;i++)
    {
        c[i + 1] += c[i] / 10;
        c[i] %= 10;
    }
    while (c[len] == 0)
        len--;
    string ans;
    for (int i = max(1,len);i >= 1;i--)
        ans += c[i];
    return ans;
}
string funb(string A,string B)
{
    int a[10005], b[10005], c[10005];
    int len = max(A.length(), B.length());
    for (int i = A.length() - 1, j = 1; i >= 0; i--, j++)
        a[j] = A[i] - '0';
    for (int i = B.length() - 1, j = 1; i >= 0; i--, j++)
        b[j] = B[i] - '0';
    for (int i = 1; i <= len; i++) {
        c[i] += a[i] + b[i];
        c[i + 1] = c[i] / 10;
        c[i] %= 10;
    }
    if (c[len + 1])
        len++;
    string ans;
    for (int i = len; i >= 1; i--)
        ans += c[i];
    return ans;
}
int main() {
    int n;
    cin >> n;
    string ans;
    for (int i = 1;i <= n;i++)
    {
        ans = funb(ans,funa(to_string(i)));
    }
    cout << ans << endl;
    return 0;
}

求大神带进题目运行一下,有ASCII码的问题,可能还有其他算法问题,请神犇们一起帮忙看一下,不用附代码,只需要问题和解决方法,代码我自己调就行,谢谢!\ 悬关


by zzy0618 @ 2024-07-18 16:30:16

@Jacky_luogu 这题是高精度数乘上低精度数,只需要将每一位按位乘上,最终处理进位

附上高精乘低精代码

for(j=0;j<100;j++)
    a[j]*=x;
for(j=0;j<100;j++)
    if(a[j]>=10)
        a[j+1]+=a[j]/10,a[j]%=10;

by Sky_Aurora @ 2024-07-18 16:35:07

@zzy0618 收到,关注你了,谢谢!


|