N!的阶乘附带简单大整数类的输入输出(暂时没有深入的了解)

Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!  

我的思路:就想着大整数类去了,才发现自己还不能很好的掌握,其实这是一个大整数与int的乘法,一个50000的数组完全可以解决,看来分析问题的能力还是比较弱呀,希望能够提升分析问题的全局能力!


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
   int n;
   int a[50000];
   while(cin>>n)
   {
        memset(a,0,sizeof(a));
        a[0]=1;
        int flag=0;
        for(int i=2;i<=n;i++)
        {
            for(int j=0;j<=flag;j++)
                  a[j]*=i;
                  for(int j=0;j<=flag;j++)
                  {
                      if(a[j]>=10)
                      {
                          a[j+1]+=a[j]/10;
                          a[j]%=10;
                      }



                  }
                  if(a[flag+1])
                    {
                        while(a[++flag]>=10)
                        {
                            a[flag+1]+=a[flag]/10;
                            a[flag]%=10;
                        }
                    }


        }
        for(int i=flag;i>=0;i--)
            cout<<a[i];
        cout<<endl;





   }



    return 0;



}


大整数类的简单输入输出,暂时就理解这些了,希望后续能够加深搞出大整数的加减乘除!

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<cstring>
 5 #include<algorithm>
 6 using namespace std;
 7 struct Big
 8 {
 9     static const int BASE=100000000;
10     static const int WIDTH=8;
11     vector<int > s;
12     Big operator =(const string &str)
13     {
14         s.clear();
15         int x,len=(str.length()-1)/WIDTH+1;
16         for(int i=0;i<len;i++)
17         {
18             int end=str.length()-i*WIDTH;
19             int start=max(0,end-WIDTH);
20             sscanf(str.substr(start,end-start).c_str(),"%d",&x);
21             s.push_back(x);
22 
23         }
24         return *this;
25 
26     }
27 
28 };
29 istream &operator >>(istream &in,Big &b)
30 {string x;
31 in>>x;
32 b=x;
33 return in;
34 }
35 
36 ostream &operator << (ostream &out, Big &x)
37 {
38     out<<x.s.back();//防止高位不足八位
39     for(int i=x.s.size()-2;i>=0;i--)
40     {
41 
42         char buf[20];
43         sprintf(buf,"%08d",x.s[i]);
44         for(int j=0;j<strlen(buf);j++)
45             out<<buf[j];
46     }
47     return out;
48 
49 }
50 int main()
51 {
52    Big a,b;
53     cin>>a>>b;
54     cout<<a<<" "<<b;
55 
56 
57     return 0;
58 
59 
60 }
原文地址:https://www.cnblogs.com/blvt/p/7220402.html