求N!(N=10000)

如果我知道 2008-09-03 12:54:23
N!这个如果N小可以用递归很容易做,但是N要是很大,就麻烦了

#include<iostream>
#include<string>
#include<vector>
#include<iomanip>
using namespace std;
int main()
{
vector<int> ival;
int num,n,addnum;
while(cin>>n)
{
num=1;
ival.clear();
ival.push_back(num);
for(int i=1;i<=n;i++)
{
addnum=0;
for(int cnt=ival.size()-1;cnt>=0;cnt--)
{
num=ival[cnt]*i;
if(cnt!=ival.size()-1)
{
num+=addnum;
addnum=0;
}
if(num>9999)
{
addnum=num/10000;
num=num% 10000;
ival[cnt]=num;
if(!cnt)
ival.insert(ival.begin(),addnum);
}
else
ival[cnt]=num;
}
}
for(int i=0;i!=ival.size();i++)
if(!i)
cout<<ival[i];
else
cout<<setfill('0')<<setw(4)<<ival[i];
cout<<endl;
}
return 0;
}



我这个代码求100!还可以,可要是10000的话,那就等很久很久结果都没出来呢!
谁能帮我改改这代码很容易的算出10000!的结果?
...全文
836 62 打赏 收藏 转发到动态 举报
写回复
用AI写文章
62 条回复
切换为时间正序
请发表友善的回复…
发表回复
a1234567yang 2008-09-28
  • 打赏
  • 举报
回复
study
avalonBBS 2008-09-08
  • 打赏
  • 举报
回复
好多牛牛啊~~
野男孩 2008-09-08
  • 打赏
  • 举报
回复
就烦你这种人!小程序弄一堆模板,出来装13.

你知道你这个递归计算10000的时候效率有多低吗?

写程序需要的踏实,不是卖弄。功能复用度高的代码用模板写就很好,这种计算程序你要是写个大数运算模板类,那也行。
就这几行P代码,还非要装13,可耻~


[Quote=引用 1 楼 yshuise 的回复:]
C/C++ code// FT.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

template <unsigned int n>
struct FT
{
enum { RET = FT<n-1>::RET * n };
};
template <>
struct FT<1>
{
enum { RET = 1 };
};

int _tmain(int argc, _TCHAR* argv[])
{
cout<<FT<30>::RET<<endl;
return 0;
}



我的代码就很快,但是要溢出,也就是这…
[/Quote]
xuxingok 2008-09-08
  • 打赏
  • 举报
回复
mark
shaolilove 2008-09-08
  • 打赏
  • 举报
回复
study!!
sw_yu 2008-09-08
  • 打赏
  • 举报
回复
受教
yueyucanyang 2008-09-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 phz1985 的回复:]
C/C++ code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int count(const int number)
{
double sum=0.0;
int i;

for(i=1; i<=number; i++)
sum+=log10((double)i);
sum=floor(sum+1);

printf("the weishu of %d! is:%d\n", number, (int)sum);

return (int)sum;
}

int main(int argc, char **argv)
{
unsigned char *c;
int num;
int cnt;
printf("Please input the n…
[/Quote]
yueyucanyang 2008-09-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yshuise 的回复:]
C/C++ code// FT.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

template <unsigned int n>
struct FT
{
enum { RET = FT<n-1>::RET * n };
};
template <>
struct FT<1>
{
enum { RET = 1 };
};
不明白是什么意思。

int _tmain(int argc, _TCHAR* argv[])
{
cout<<FT<30>::RET<<endl;
return 0;
}



我的代码就很快,但是要溢出,也就是这…
[/Quote]
exgalary 2008-09-08
  • 打赏
  • 举报
回复
<2s. E6550+2G.

#include <stdio.h>
#include <string.h>

int compute_jiecheng(int n, int *arr)
{
int i, j, jing, last_index=0;

for (i=2;i<=n;i++) {
jing=0;
for(j=0;j<=last_index;j++) {
arr[j]=arr[j]*i;
arr[j]+=jing;
jing=0;
if (arr[j]>=1000) {
jing=arr[j]/1000;
arr[j]=arr[j]-1000*jing;
}
}
if (jing) {
arr[j]+=jing;
last_index++;
}
}
return last_index;
}

void format_print(int num)
{
if (num < 10) {
printf("00%d", num);
} else if (num < 100) {
printf("0%d", num);
} else {
printf("%d", num);
}
}

int main()
{
int i, arr[15000]={0};
arr[0]=1;
i=compute_jiecheng(100, arr);
while(i>=0) {
format_print(arr[i]);
i--;
}
return 0;
}
tangshuiling 2008-09-07
  • 打赏
  • 举报
回复
mark!
member 2008-09-07
  • 打赏
  • 举报
回复
mark
Vegertar 2008-09-06
  • 打赏
  • 举报
回复
先MARK
K行天下 2008-09-06
  • 打赏
  • 举报
回复

#include <stdio.h>
#define max 32767
int main()//把这个算法增加处理后面0的情况就是二楼链接的算法了
{
int i,j,k,x,n=0,sum=0,a[max+1];
printf("输入要求阶乘的数:");
while(scanf("%d", &n))
{
k=max;
a[k]=1;//先乘以1
for(i=2;i<=n;i++)//从2乘到n
{
x=0;
for(j=max;j>=k;j--)//max-k+1标记当前的位数
{//将n与现存的各位相乘,更新结果
x=a[j]*i+x;
a[j]=x%10;
x=x/10;
}
while(x>0)//有进位的情况下,k减
{
k--;
a[k]=x%10;
x=x/10;
}
}//结束计算阶乘
for(i=k;i<=max;i++)
printf("%d", a[i]);
printf("\n输入要求阶乘的数:");
}
return 0;
}

CA_HA_M 2008-09-06
  • 打赏
  • 举报
回复
用循环就好,干嘛用递归,不慢才怪
frisky_lobo 2008-09-06
  • 打赏
  • 举报
回复
[Quote=引用楼主 liaohongwei 的帖子:]
N!这个如果N小可以用递归很容易做,但是N要是很大,就麻烦了
[/Quote]
你在写什么?如此复杂?N大了定义成LONG LONG不久搞定了?
yshuise 2008-09-06
  • 打赏
  • 举报
回复
不对,我刚才说错了,上面的编译错误是1楼的 

可能我太菜了,15楼的代码我看的不是很懂,编译后运行时没有运行结果(我等很就都没出来)。


老大,这个是vc的头文件,VC工程都有的。
whuyotc 2008-09-06
  • 打赏
  • 举报
回复
用的也是VS2008,但是怎么执行的时候出错误了呀?
[Quote=引用 15 楼 akirya 的回复:]
VC2008 2秒左右
cl /O2 filename.cpp
g++ 不到2秒
g++ -O3 filename.cpp


C/C++ code#include <algorithm>
#include <vector>
#include <cstdio>
#include<functional>
typedef unsigned int Type;
enum{ BASE_DATA = 10000, MAX_NUM = 10000 , MAX_SIZE = MAX_NUM+1000};
struct ConverData
{
inline Type operator()(Type x)
{
Type y = (x/1000);
x %= 1000;

[/Quote]
whuyotc 2008-09-06
  • 打赏
  • 举报
回复
44楼的程序编译速度执行速度都是好快啊

whuyotc 2008-09-06
  • 打赏
  • 举报
回复
学习中
帅得不敢出门 2008-09-06
  • 打赏
  • 举报
回复
mark
加载更多回复(42)

65,206

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧