大佬们为什么我写的高精前面会出现好几个零?

P1009 [NOIP1998 普及组] 阶乘之和

haoyelll @ 2022-12-04 20:36:07

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
    int n,  a[1001] = { 1 }, b[1001] = { 1 }, i, j;
    scanf("%d", &n);
    for (i = 2; i <= n; i++)
    {
        for (j = 0; j <= i; j++)
            b[j] *= i;
        for (j = 0; j <= i; j++)
        {
            if (b[j] >9)
            {
                b[j + 1] += b[j] / 10;
                b[j] %= 10;
            }
        }       
        for (j = 0; j <= i; j++)
        {
            a[j] += b[j];
            if (a[j] >9)
            {
                a[j + 1] += a[j] / 10;
                a[j] %= 10;
            }
        }
    }
    for (i;i>0&& a[i] == 0; i--)
        for (j = i; j >= 0; j--)
            printf("%d",a[j]);
    return 0;
}

by 安舒阳 @ 2022-12-04 20:40:08

@haoyelll 因为你需要在输出中去掉这些0


by haoyelll @ 2022-12-04 20:51:02

@安舒阳 大概懂了,谢谢大佬


by ysxkrs @ 2022-12-04 21:05:11

高精度乘法你可能还有一些不太懂,可以复习一下


by ysxkrs @ 2022-12-04 21:12:52

本蒟蒻的伪代码,仅供参考,勿喷

int a[], alen ;
int b[], blen ;
int ans[], anslen = alen + blen ;
for ( int i = 1, x; i <= alen; i ++ )
    x = 0 ;
    for ( int j = 1; j <= blen; j ++ )
        ans[i + j - 1] += a[i] * b[i] + x ;
        x = ans[i + j - 1] / 10 ;
        ans[i + j - 1] %= 10 ;
    ans[blen + i] = x ;
while ( ans[anslen] == 0 && anslen > 0 ) anslen -- ;

by ysxkrs @ 2022-12-04 21:23:51

参考博客


by haoyelll @ 2022-12-04 22:31:54

@xhlmrdr 好的,谢谢大佬


|