
by 雨中飞燕 QQ:78803110 QQ讨论群:5305909



[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
C/C++算法习题(OnlineJudge):[url]http://yzfy.org/[/url]
在c++里 int这类自然数据类型是不能是不能重新定义int类的,要根据不同的程序目的自己编写,下面有个求阶乘的例子做参考:
#include<iostream>
using namespace std;
#define max 200
void ditui(int a[],int k)
{
int m=a[0]; //m代表计算结果的位数
int *b=new int[m+1];
int i,j;
int r;
int carry;
for(i=1;i<=m;i++) b[i]=a[i];
for(j=1;j<k;j++)
{
for(carry=0,i=1;i<=m;i++)
{
r=(i<=a[0]?a[i]+b[i]:a[i])+carry;
a[i]=r%10;carry=r/10;
}
if(carry) a[++m]=carry;
}
delete [] b;
a[0]=m;
}
void write(int* a,int k)
{
int i;
cout<<k<<"!=";
for(i=a[0];i>0;i--)
cout<<a[i];
cout<<endl;
}
void main()
{
int a[max],n,k;
cout<<"输入n的值:"<<endl;
cin>>n;
a[0]=1;a[1]=1;
write(a,1);
for(k=2;k<=n;k++)
{
ditui(a,k);
write(a,k);
}
}
If your int fits 32-bit int/unsigned int, you can use the built-in type 'int' or 'unsigned int';
If your int fits 64-bit int/unsigned int, you can use type 'long long' or 'unsigned long long';
If your int exceeds 64-bit, you need a 'BigInt' class. Java has a built-in BigInt class, C++ does not.