阶乘 大数存储

 1 #include<stdio.h>
 2 #define N 1000
 3  
 4 int Factorial(int n, int a[], int index);
 5 int main()
 6 {
 7     int a[N] = {0,1},index = 1;
 8     for(int n = 1; n<=100; ++n)
 9     {
10         index = Factorial(n, a, index);
11         printf("%d! = ", n);
12         for(int i=index; i>0; i--)
13         {
14             printf("%d", a[i]);
15         }
16         printf("\n");
17     }
18     
19     return 0;
20 }
21  
22 int Factorial(int n, int a[], int index)
23 {
24     int carry = 0;/**< 进位数 */
25     for(int j=1; j<=index; j++)/**< 乘数与被乘数,从第一位开始,逐位相乘 */
26     {
27         int temp = n*a[j] + carry;/**< 乘积 + 进位数 */
28         a[j] = temp%10;/**< 乘积 + 进位数,保留个位 */
29         carry = temp/10;/**< 进位 */
30     }
31     while(carry > 0)/**< 进位数大于0,最高位++,最高位保留进位数个位 */
32     {
33         a[++index] = carry%10;
34         carry /= 10;
35     }
36     return index;
37 }

猜你喜欢

转载自www.cnblogs.com/GoldenEllipsis/p/10443350.html