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;


}


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

 #include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
struct Big
{
static const int BASE=;
static const int WIDTH=;
vector<int > s;
Big operator =(const string &str)
{
s.clear();
int x,len=(str.length()-)/WIDTH+;
for(int i=;i<len;i++)
{
int end=str.length()-i*WIDTH;
int start=max(,end-WIDTH);
sscanf(str.substr(start,end-start).c_str(),"%d",&x);
s.push_back(x); }
return *this; } };
istream &operator >>(istream &in,Big &b)
{string x;
in>>x;
b=x;
return in;
} ostream &operator << (ostream &out, Big &x)
{
out<<x.s.back();//防止高位不足八位
for(int i=x.s.size()-;i>=;i--)
{ char buf[];
sprintf(buf,"%08d",x.s[i]);
for(int j=;j<strlen(buf);j++)
out<<buf[j];
}
return out; }
int main()
{
Big a,b;
cin>>a>>b;
cout<<a<<" "<<b; return ; }
上一篇:大整数乘法(Comba 乘法 (Comba  Multiplication)原理)


下一篇:7.8 C++容器适配器