为什么只有50分,哪里需要改进?

P1009 [NOIP1998 普及组] 阶乘之和

qimiaojiang @ 2023-10-29 20:19:12


#include<stdio.h>
int main()
{
    int  n,a,b;
    unsigned int c;
    c<=50;
    c>=0;
    long long int S = 0;
    scanf("%d", &c);
    for (n=1; n <= c; n++) {
        b = 1;
        for (a = 1; a <= n ; a++) {
            b = b * a;
        }
        S = S + b;
    }
    printf("%lld", S);
    return 0;
}

by heyx0201 @ 2023-10-29 20:23:17

@qimiaojiang 没见过这种的。。。一道高精度题直接开 int。。。int 范围多少不知道吗。。


by qimiaojiang @ 2023-10-29 20:30:59

@heyx0201 给b定义成long long int也不行,但用double也不行,怎么办?


by heyx0201 @ 2023-10-29 20:35:15

@qimiaojiang 高精度


by heyx0201 @ 2023-10-29 20:35:34

@qimiaojiang 关于高精度是什么的问题:自己 bdfs


by qimiaojiang @ 2023-10-29 20:37:14

更新了一下代码,能在上运行,但是在这说是错的

#include<stdio.h>
int main()
{
    int  n,a;
    unsigned int c;
    c<=50;
    c>=0;
    double S = 0,b;
    scanf("%d", &c);
    for (n=1; n <= c; n++) {
        b = 1;
        for (a = 1; a <= n ; a++) {
            b = b * a;
        }
        S = S + b;
    }
    printf("%lf", S);
    return 0;
}

by laozhang_123 @ 2023-10-30 15:11:32

......

#include<bits/stdc++.h>
#define maxn 100
using namespace std;
struct Bigint{
    int len,a[maxn];
    Bigint(int x=0){
        memset(a,0,sizeof(a));
        for(len=1;x;len++)
            a[len]=x%10,x/=10;
        len--;
    }
    int &operator[](int i){
        return a[i];
    }
    void flatten(int L){
        len=L;
        for(int i=1;i<=len;i++)
            a[i+1]+=a[i]/10,a[i]%=10;
        for(;!a[len];) len--;
    }
    void print(){
        for(int i=max(len,1);i>=1;i--)
            printf("%d",a[i]);
    }
}; 
Bigint operator+(Bigint a,Bigint b){
    Bigint c;
    int len=max(a.len,b.len);
    for(int i=1;i<=len;i++)
        c[i]=a[i]+b[i];
    c.flatten(len+1);
    return c;
}
Bigint operator*(Bigint a,int b){
    Bigint c;
    int len=a.len;
    for(int i=1;i<=len;i++)
        c[i]=a[i]*b;
    c.flatten(len+11);
    return c;
}
int main(){
    Bigint ans(0),fac(1);
    int m;
    cin>>m;
    if(m==0){
        cout<<1;
        return 0;}
    for(int i=1;i<=m;i++){
        fac=fac*i;
        ans=ans+fac;
    }
    ans.print();
    return 0;
} 

by heyx0201 @ 2023-10-31 21:43:09

@laozhang_123 你就没有看出来他用的是C语言吗


by lijinhao666 @ 2023-11-01 17:09:14

@heyx0201 我不知道


by FIGFUH001 @ 2023-11-04 10:52:14

《高精》


|