求助全WA

P1009 [NOIP1998 普及组] 阶乘之和

轮回·天照 @ 2022-07-27 09:55:06

rt,样例输出100


by 轮回·天照 @ 2022-07-27 09:55:14

#include <bits/stdc++.h>
using namespace std;
namespace NUM{
    const int MAX_SIZE = 20010;
    struct num{
        int l = 0,f = 1;
        int nums[MAX_SIZE];
        num(){}
        num(int n1){
            memset(nums,0,sizeof(nums));
            l = 0,f = 1;
            if(n1 == 0)
                l = 1;
            if(n1 < 0)
                n1 = -n1,f = -1;
            while(n1){
                nums[l++] = n1 % 10;
                n1 /= 10;
            }
        }
        num(const num &n1){
            memset(nums,0,sizeof(nums));
            l = n1.l,f = n1.f;
            for(int i = 0;i < l;i++)
                nums[i] = n1.nums[i];
        }
    };
    /* ---------- */
    void read(num &n1){
        memset(n1.nums,0,sizeof(n1.nums));
        n1.l = 0;
        int a[MAX_SIZE];
        char c = getchar();
        while(!('0' <= c && c <= '9')){
            if(c == '-')
                n1.f = -1;
            c = getchar();
        }
        while('0' <= c && c <= '9'){
            a[n1.l++] = c - '0';
            c = getchar();
        }
        for(int i = 0;i < n1.l;i++)
            n1.nums[i] = a[n1.l - i - 1];
    }
    void write(num n1){
        if(n1.f == -1)
            putchar('-');
        for(int i = n1.l - 1;i >= 0;i--)
            putchar(n1.nums[i] + '0');
    }
    num abs(num n1){
        n1.f = 1;
        return n1;
    }
    /* ---------- */
    bool operator ==(num n1,num n2){
        if(n1.f != n2.f)
            return 0;
        if(n1.l != n2.l)
            return 0;
        for(int i = 0;i < n1.l;i++)
            if(n1.nums[i] != n2.nums[i])
                return 0;
        return 1;
    }
    bool operator !=(num n1,num n2){
        return !(n1 == n2);
    }
    bool operator <(num n1,num n2){
        if(n1.f != n2.f)
            return n1.f < n2.f;
        if(n1.l != n2.l)
            return n1.l < n2.l;
        for(int i = 0;i < n1.l;i++)
            if(n1.nums[i] != n2.nums[i])
                return n1.nums[i] < n2.nums[i];
        return 0;
    }
    bool operator >(num n1,num n2){
        if(n1.f != n2.f)
            return n1.f > n2.f;
        if(n1.l != n2.l)
            return n1.l > n2.l;
        for(int i = 0;i < n1.l;i++)
            if(n1.nums[i] != n2.nums[i])
                return n1.nums[i] > n2.nums[i];
        return 0;
    }
    bool operator <=(num n1,num n2){
        return n1 < n2 || n1 == n2;
    }
    bool operator >=(num n1,num n2){
        return n1 > n2 || n1 == n2;
    }
    /* ---------- */
    num operator +(num n1,num n2){
        if(abs(n1) < abs(n2))
            return n2 + n1;
        if(n1.f == n2.f){
            num n3;
            int l1 = n1.l,l2 = n2.l;
            int len = max(l1,l2);
            for(int i = 0;i < len;i++){
                n3.nums[i] = n3.nums[i] + n1.nums[i] + n2.nums[i];
                n3.nums[i+1] += n3.nums[i] / 10;
                n3.nums[i] %= 10;
            }
            if(n3.nums[len])
                len++;
            n3.l = len;
            n3.f = n1.f;
            if(n3 == num(0))
                n3.f = 1;
            return n3;      
        }
        else{
            num n3;
            int l1 = n1.l,l2 = n2.l;
            int len = max(l1,l2);
            int t = 0;
            for(int i = 0;i < len;i++){
                t += n1.nums[i] - n2.nums[i];
                n3.nums[n3.l++] = (t + 10) % 10;
                if(t < 0)
                    t = -1;
                else
                    t = 0;
            }
            while(n3.nums[n3.l - 1] == 0 && n3.l > 1)
                n3.l--;
            n3.f = n1.f;
            if(n3 == num(0))
                n3.f = 1;
            return n3;
        }
    }
    num operator -(num n1,num n2){
        n2.f = -n2.f;
        return n1 + n2;
    }
    num operator *(num n1,num n2){
        if(abs(n1) == num(0) || abs(n2) == num(0))
            return num(0);
        if(abs(n1) < abs(n2))
            return n2 * n1;
        bool flag = 0;
        if(n1.f != n2.f)
            flag = 1;
        n1.f = 1,n2.f = 1;
        num n3;
        n3.f = 1;
        int l1 = n1.l,l2 = n2.l,len = l1 + l2;
        for(int i = 0;i < l1;i++){
            for(int j = 0;j < l2;j++){
                n3.nums[i + j] += n1.nums[i] * n2.nums[j];
                n3.nums[i + j + 1] += n3.nums[i + j] / 10;
                n3.nums[i + j] %= 10;
            }
        }
        while(n3.nums[len - 1] == 0 && len > 1)
            len--;
        n3.l = len;
        if(flag)
            n3.f = -1;
        return n3;
    }
//  num operator /(num n1,num n2){
//      if(abs(n2) == num(0)){
//          fprintf(stderr,"ERROR:DIVIDED BY ZERO!\n");
//          throw 1;            
//      }
//      if(abs(n1) == num(0) || abs(n1) < abs(n2))
//          return num(0);
//      bool flag = 0;
//      if(n1.f != n2.f)
//          flag = 1;
//      n1.f = 1,n2.f = 1;
//      num n3 = 0;
//      while(n1 > n2){
//          n1 = n1 - n2;  // ERROR!
//          n3 = n3 + num(1);
//      }
//      n3 = n3 - num(1);
//      if(flag)
//          n3.f = -1;
//      return n3;
//  }
//  num operator %(num n1,num n2){
//      return n1 - n2 * (n1 / n2);
//  }
}
using NUM::num;
int n;
num n1 = 0;
int main(){
    cin >> n;
    for(int i = 1;i <= n;i++){
        num n2 = 1;
        for(int j = 1;j <= i;j++)
            n2 = n2 * num(j);
        cout << endl;
    }
    write(n1);
    return 0;
}

by chlchl @ 2022-07-27 09:57:28

为什么您的这么长……

别误会,我说的是代码。


by 轮回·天照 @ 2022-07-27 10:00:11

@caihaolang 这是没写完的高精度模板,里面还有减乘和大于小于等其他东西,所以很长

主要原因还是我太蒻了


by chlchl @ 2022-07-27 10:04:04

@轮回·天照 别这样,很乱的。


by 轮回·天照 @ 2022-07-27 10:07:02

@caihaolang az


by space_sea @ 2022-08-12 20:06:43

看着我的代码,我无语了

#include <bits/stdc++.h>
using namespace std;
int a[1001], b[1001], lena=1, lenb, x, n;
int main(){
    cin >> n;
    a[1]={1};
    for (int i=1; i<=n; i++){
        x=0;

        for (int j=1; j<=lena; j++){
            a[j]=a[j]*i+x;
            x=a[j]/10;
            a[j]%=10;

        }
        while (x>0){
            a[++lena]=x%10;
            x/=10;

        }
        lenb=max(lena, lenb);
        for (int j=1; j<=lenb; j++){
            b[j]+=a[j]+x;
            x=b[j]/10;
            b[j]%=10;

        }
        if (x>0){
            b[lenb]=x;

        }
    }
    for (int i=lenb; i>0; i--){
        cout << b[i];

    }

    return 0;
}

注:仅供参考,不准抄袭!!!


|