您的位置:首页 > 其它

求1000!(通过数组模拟大数)

2013-03-27 19:56 127 查看
#include<iostream>
#include<ctime>
using namespace std;
void main()
{
int Data[6000];               //存储6000位数的整数数组
int Digit;                    //数据位数
for(int i = 1;i < 6000;i++)   //数组初始化
Data[i] = 0;
Data[0] = 1;
Data[1] = 1;
Digit = 1;
cout<<"输入你想要计算的整数:";
int N;
cin>>N;

double start = clock();
int index2 = 0;   //统计2的数目
for(i = 1;i < N + 1;i++)
{
for(int j = 1;j < Digit + 1;j++)                  //数组中元素的运算
Data[j] *= i;
for(j = 1;j < Digit + 1;j++)
{
if(Data[j] >= 10)                             //进位处理
{
for(int r = 1;r < Digit + 1;r++)
{
if(Data[Digit] >= 10)
Digit++;
Data[r+1] += Data[r]/10;
Data[r] %= 10;
}
}
}
if(i == N)         //求出N的阶乘
{
cout<<i<<"! =";
for(int k = Digit;k > 0;k--)
{
cout<<Data[k];
if(Data[k] == 2)
index2++;
}
cout<<endl;
}
}

cout<<endl<<N<<"!中2的数目共有"<<index2<<"个"<<endl;
long end = clock();
cout<<endl<<"运行时间:"<<(end-start)/1000<<"s"<<endl;
}

/*
输入你想要计算的整数:1000
1000! =4023872600770937735437024339230039857193748642107146325437999104299385123
98629020592044208486969404800479988610197196058631666872994808558901323829669944
59099742450408707375991882362772718873251977950595099527612087497546249704360141
82780946464962910563938874378864873371191810458257836478499770124766328898359557
35432513185323958463075557409114262417474349347553428646576611667797396668820291
20737914385371958824980812686783837455973174613608537953452422158659320192809087
82973084313928444032812315586110369768013573042161687476096758713483120254785893
20767169132448426236131412508780208000261683151027341827977704784635868170164365
02415369139828126481021309276124489635992870511496497541990934222156683257208082
13331861168115536158365469840467089756029009505376164758477284218896796462449451
60765353408198901385442487984959953319101723355556602139450399736280750137837615
30712776192684903435262520001588853514733161170210396817592151090778801939317811
41945452572238655414610628921879602238389714760885062768629671466746975629112340
82439208160153780889893964518263243671616762179168909779911903754031274622289988
00519544441428201218736174599264295658174662830295557029902432415318161721046583
20367869061172601587835207515162842255402651704833042261439742869330616908979684
82590125458327168226458066526769958652682272807075781391858178889652208164348344
82599326604336766017699961283186078838615027946595513115655203609398818061213855
86003014356945272242063446317974605946825731037900840244324384656572450144028218
85252470935190620929023136493273497565513958720559654228749774011413346962715422
84586237738753823048386568897646192738381490014076731044664025989949022222176590
43399018860185665264850617997023561938970178600408118897299183110211712298459016
41921068884387121855646124960798722908519296819372388642614839657382291123125024
18664935314397013742853192664987533721894069428143411852015801412334482801505139
96942901534830776445690990731524332782882698646027898643211390835062170950025973
89863554277196742822248757586765752344220207573630569498825087968928162753848863
39690995982628095612145099487170124451646126037902930912088908694202851064018215
43994571568059418727489980942547421735824010636774045957417851608292301353580818
40096996372524230560855903700624271243416909004153690105933983835777939410970027
75347200000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000

1000!中2的数目共有248个

运行时间:0.329s
*/


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: