请大佬帮忙看看奇特的错误怎么改

P1009 [NOIP1998 普及组] 阶乘之和

LiJinLin_AFO @ 2024-02-13 19:25:27

#include<iostream>
#include<string>
#include<string.h>
using namespace std;
string ans="1",link;
int c[4005];
inline void reverse(string &x){
    string a="";
    for(int i=x.size()-1;i>=0;i--) a.push_back(x[i]);
    x=a;
}//翻转字符串
inline string ntos(int n){
    if(!n) return "";
    string s=ntos(n/10);
    s.push_back(n%10+'0');
    return s;
}//将数字按从高位到低位顺序转为字符串
inline void add(){
    string s="";
    int n=ans.size();
    ans.push_back('0');
    for(int i=link.size();i<=n;i++) link.push_back('0');
    for(int i=0,f=0;i<n||i>=n&&f==1;i++){
        int x=ans[i]-'0',y=link[i]-'0';
        int z=x+y+f;
        if(z>9) z-=10,f=1;
        else f=0;
        s.push_back(z+'0');
    }
    ans=s;
}//高精字符串ans加上字符串link(均为数字)
inline void print(){
    int n=ans.size()-1;
    while(n>=0&&ans[n]=='0') --n;//去除前导零
    for(int i=n;i>=0;i--) putchar(ans[i]);
}//输出高精数字
inline string work(string a,string b){
    int n=a.size(),m=b.size();
    reverse(a),reverse(b);
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            int x=a[i]-'0',y=b[j]-'0';
            c[i+j]+=x*y;
        }
    }
    for(int i=0;i<n+m-1;i++){
        int yu=c[i]/10;
        c[i]%=10;
        c[i+1]+=yu;
    }
    int st=n+m-2;
    for(;st>=0&&c[st]==0;st--);
    if(st<0){
        putchar('0');
        return 0;
    }
    string qwert="";
    for(int i=st;i>=0;i--) qwert.push_back(c[i]);
    memset(c,0,sizeof c);
    return qwert;
}//高精乘
int main(){
    int n;
    scanf("%d",&n);
    for(int i=2;i<=n;i++,add(),link="1"){
        for(int j=2;j<=i;j++){
            link=work(link,ntos(j));
        }
    }
    print();
    return 0;
}

请大佬帮忙看看,估计是 CE 了,蒟蒻从未见过这种情况,玄关......


by LiJinLin_AFO @ 2024-02-13 19:38:08


by LiJinLin_AFO @ 2024-02-13 19:40:19


by Chenliy @ 2024-02-13 19:55:02

您在函数里 return 0 了


by Chenliy @ 2024-02-13 19:55:26

work 函数类型是string


by Chenliy @ 2024-02-13 19:55:46

@LiJinLin_AFO


by LiJinLin_AFO @ 2024-02-13 20:09:08

@Chenliy 感谢大佬的指点,至少不是 RE 了,不过还是一片 WA,希望能再次指点一下,非常感谢!(已关)


by Chenliy @ 2024-02-13 20:35:41

#include<iostream>
#include<string>
#include<string.h>
using namespace std;
string ans="1",link;
int c[4005];
inline void reverse(string &x){
    string a="";
    for(int i=x.size()-1;i>=0;i--) a.push_back(x[i]);
    x=a;
}//翻转字符串
inline string ntos(int n){
    if(!n) return "";
    string s=ntos(n/10);
    s.push_back(n%10+'0');
//  cout<<" "<<n<<" "<<s.size()<<endl;
    return s;
}//将数字按从高位到低位顺序转为字符串
inline void print(){
    int n=ans.size()-1;
    while(n>=0&&ans[n]=='0') --n;//去除前导零
    for(int i=n;i>=0;i--) cout<<ans[i]; cout<<endl;
}//输出高精数字
inline void add(){
    string s="";
    reverse(link);
    int n=max(ans.size(),link.size())+1;
    for(int i=ans.size();i<=n;i++) ans.push_back('0');
    for(int i=link.size();i<=n;i++) link.push_back('0');
    for(int i=0,f=0;i<n||i>=n&&f==1;i++){
        int x=ans[i]-'0',y=link[i]-'0';
        int z=x+y+f;
        if(z>9) z-=10,f=1;
        else f=0;
        s.push_back(z+'0');
    }
    ans=s;
    //print();
}//高精字符串ans加上字符串link(均为数字)
inline string work(string a,string b){
    int n=a.size(),m=b.size();
//  for(int i=0;i<n;i++) cout<<a[i]-'0'<<" "; cout<<endl;
//  for(int i=0;i<m;i++) cout<<b[i]-'0'<<" "; cout<<endl;
    reverse(a),reverse(b);
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            int x=a[i]-'0',y=b[j]-'0';
            c[i+j]+=x*y;
        }
    }
    for(int i=0;i<n+m-1;i++){
        int yu=c[i]/10;
        c[i]%=10;
        c[i+1]+=yu;
    }
    int st=n+m-1;
    for(;st>=0&&c[st]==0;st--);
    if(st<0){
        putchar('0');
        exit(0);
    }
    //for(int i=st;i>=0;i--) cout<<c[i]<<" "; cout<<endl;
    string qwert="";
    for(int i=st;i>=0;i--) qwert.push_back(c[i]+'0');
    memset(c,0,sizeof c);
    return qwert;
}//高精乘
int main(){
    int n;
    scanf("%d",&n);
    link=ntos(1);
    for(int i=2;i<=n;i++){
        for(int j=2;j<=i;j++){
            link=work(link,ntos(j));
        }
        add(),link=ntos(1);
    }
    print();
    return 0;
}

改动有些大……


by Chenliy @ 2024-02-13 20:37:17

@LiJinLin_AFO


by LiJinLin_AFO @ 2024-02-13 21:06:51

@Chenliy 感谢大佬!!!


|