题目不清不楚的
scanf 再 printf 不就行了
最好的域名、虚拟主机、VPS www.
挺麻烦的,写了40分钟,俺的程序算到98的阶乘的时候crash,因为strlen已经不顶用了,必须把string拆成N结。
用C++写就不用考虑这问题了。把程序里所有的char[]换成string 或CString就应该多算不少了。
void Add (char charar1[], char charar2[])
{
int intCount, add1, add2, sum, intLongPos, intTmpPos;
char chararTmpLong[100000], *pcharShort;
if (strlen(charar2) > strlen(charar1))
strcpy(chararTmpLong, charar2), pcharShort = &charar1[0];
else
strcpy(chararTmpLong, charar1), pcharShort = &charar2[0];
for (intCount = strlen(pcharShort)-1; intCount >= 0; intCount--)
{
intLongPos = strlen(chararTmpLong) - 1 - (strlen(pcharShort)-1 - intCount);
add1 = chararTmpLong[intLongPos] - '0';
add2 = pcharShort[intCount] - '0';
sum = add1 + add2;
chararTmpLong[intLongPos] = '0' + sum % 10;
if (sum >= 10)
{
intTmpPos = intLongPos;
while (intTmpPos > 0)
{
chararTmpLong[--intTmpPos]++;
if ((chararTmpLong[intTmpPos] - '0') < 10)
break;
chararTmpLong[intTmpPos] = '0';
chararTmpLong[--intTmpPos]++;
}
}
}
if ((chararTmpLong[0] - '0') < 10)
strcpy(charar1, chararTmpLong);
else
{
sprintf(charar1, "1%s", chararTmpLong);
charar1[1] -= 10;
}
}
void Multiply(int intNumber, char result[])
{
char chararNew[100000] = "0", chararTmp[100000];
int intCount, intBit;
for (intCount = strlen(result)-1; intCount >= 0; intCount--)
{
sprintf(chararTmp, "%d", (result[intCount]-'0')* intNumber);
for (intBit = intCount; intBit < strlen(result)-1; intBit++)
strcat(chararTmp, "0");
if (intNumber == 98 && intCount == 88)
intBit++;
Add(chararNew, chararTmp);
}
strcpy(result, chararNew);
};
int main(int argc, char* argv[])
{
char charar[100000] = "1";
for (int intCount = 2; intCount <= 97; intCount++)
Multiply (intCount, charar);
return 0;
}
太大了,会产生溢出。实现阶乘的方法简单。
#include "stdio.h"
#include "conio.h"
main()
{
int i;
scanf("%d",&i);
if(i>=16)
printf("溢出\n");
else
{fact();
printf("%d!=%d\n",i,fact(i));}
getch();
}
/*递归求阶乘的子模块*/
int fact(j)
int j;
{
int sum;
if(j==0)
sum=1;
else
sum=j*fact(j-1);
return sum;
}