求:sum=1*2*3*4*5*..........*n
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 400
void getResult(char *result,char *temp){
char str[MAX+10] = {0};
int i,m,n,k,y;
unsigned int j;
for(i = strlen(temp) - 1,k = 0; i >= 0; k ++,i --)
for(y = k, j = 0; j < strlen(result); y ++,j ++){
m = (temp[i] - '0') * (result[j] - '0');
n = y;
do{
if(str[n] == '\0'){
str[n] = m % 10 + '0';
if(m / 10)
str[n+1] = m / 10 + '0';
break;
}
m = (m + str[n] - '0');
str[n] = m % 10 + '0';
m = m / 10;
n ++;
}while(m);
}
if(strlen(str) >= MAX){
puts("your should make the macro MAX bigger !");
system("pause");
exit(1);
}
strcpy(result,str);
}
int main(void){
char result[MAX] = {0};
char strTemp[4];
int i;
int n;
strcpy(result,"1");
printf("The program will computer(n!)\nPlease enter n:");
scanf("%d",&n);
for(i = 1; i <= n; i ++){
itoa(i,strTemp,10);
getResult(result,strTemp);
}
printf("(%d!)=",n,strlen(result));
for(i = strlen(result) - 1; i >= 0; i --)
putchar(result[i]);
putchar('\n');
return 0;
}
[此贴子已经被作者于2007-3-19 11:26:46编辑过]