求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 */
相关文章推荐
- 大数乘法(转载:通过C语言数组模拟)
- 简单n! (本来只是一个·简单的地推,只是大数问题有点纠结·,本人用数组简单模拟了一下)
- POJ 2506 Tiling(大数递推&&(数组模拟||JAVA))
- C++通过自定义函数找出一个整数数组中第二大数的方法
- SQL Server中通过拆分字符串来模拟数组
- hdu 1042 N! 大数用数组模拟
- HDU 1047(大数相加)(数组模拟)
- SQL Server中通过拆分字符串来模拟数组
- HDU 1753 大明A+B(大数加法,数组模拟)
- 51nod oj 1027 大数乘法【数组模拟】
- 转载http://blog.csdn.net/yxnk:模拟大数乘法,将结果的每5位存在数组a[]中,以提高运算速
- 用数组模拟大数加法模板
- 通过实例模拟ASP.NET MVC的Model绑定机制:数组
- 求斐波那契数列第100位(通过C++数组模拟大数,注意VC6语法支持的不好,编译不过,在VS下面可以)
- 通过实例模拟ASP.NET MVC的Model绑定机制:数组
- 2018_2_1_product_数组模拟_大数乘
- 写了一个Windows服务,通过C#模拟网站用户登录并爬取BUG列表查询有没有新的BUG,并提醒我
- [置顶] 路由器密码和上网账号密码都忘记了怎么办,可以通过模拟PPPoE协议从路由器获取
- unity 通过摄像机模拟实现小地图
- 【iOS开发-44】通过案例谈iOS代码重构:合并、格式化输出、宏变量、利用数组字典存储数据,以及利用plist的终极知识