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 }
时间: 2024-07-30 10:13:28

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

初涉算法——大整数类

处理方法:用数组存储整数. C++好处:重载运算符,使大整数类可以像int一样使用. 结构体BigInteger可用于储存高精度非负整数: 1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include<iostream> 5 using namespace std; 6 7 struct BigInteger { 8 static const int BASE = 100000000

大整数类

大整数类又叫高精度. 就是求大数的四则运算的算法, (其实就是模拟小学生算数的方法, 什么? 你不会, 那你还不如小学生, 哈哈!). 在这里只贴加法运算符的重载,其他的运算符与加法类似.闲言少叙, 直接上代码(小声告诉你, 里面用了几个库函数和STL, 嘿嘿!!!). 1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include<iostream> 5 using namespac

大整数类BIGN的设计与实现 C++高精度模板

首先感谢刘汝佳所著的<算法竞赛入门经典>. 众所周知,C++中储存能力最大的unsigned long long 也是有着一个上限,如果我们想计算非常大的整数时,就不知所措了,所以,我写了一个高精度类,允许大整数的四则运算 这个类利用字符串进行输入输出,并利用数组进行储存与处理,通过模拟四则运算,可以计算很大的整数的加减乘除比大小. 贴上我的代码: #include<string> #include<iostream> #include<iosfwd> #i

用Java的大整数类Integer来实现大整数的一些运算

import java.io.*; import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); BigInteger a, b; while(sc.hasNext()) { a = sc.nextBigInteger(); b = sc.nextBigInteger(); Syste

定义一个大整数类,并重载乘法*运算符

1 struct bigint{ 2 int a[500]; //可表示1000位以内的整数 3 bigint& operator*(int & value){ 4 for(int i=0;i<300;i++) 5 a[i]=a[i]*value; 6 int c=0; 7 for(int i=0;i<300;i++) 8 { 9 a[i]=a[i]+c; 10 c=a[i]/100; 11 a[i]=a[i]%100; 12 } 13 return *this; 14 } 1

大整数类(已完善)

在这里吐槽CSDN,代码保存在CSDN上面,复制下来变成一堆乱码,垃圾的要死.以后慢慢转博客园 偷偷告诉你们,python的的大数可以像c语言的int类型使用,打算学python了 #include<iostream> #include<cstring> #include<iomanip> #include<algorithm> #include<cstdlib> #include<cstdio> using namespace st

uva 10862 Connect the Cable Wires大整数类c++

1: 1 2:1+(1+1) 3:1+2+(2+3) 4:1+2+5+(5+8) 而斐波那契数列1 1 2 3 5 8…… 因此推出a[n]=a[n-1]+fib[2*i-1]+fib[2*1-2]; java代码 import java.util.*; import java.math.*; public class Main { public static void main(String args[]){ BigInteger [] ans=new BigInteger[2010]; an

大整数类模板

#include <iostream> #include <cstdio> #include <sstream> #include <vector> using namespace std; struct BigInteger { static const int BASE=100000000; static const int WIDTH=8; vector<int> s; BigInteger(long long num=0) { *this

HDU1042 N!(大整数类应用)

Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one line, process to the end of file. Output For each N, output N! in one line. Sample Input 1 2 3 Sample Output 1 2 6 Code: #include <iostream> #incl