题目1076:N的阶乘
生活随笔
收集整理的這篇文章主要介紹了
题目1076:N的阶乘
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述: 輸入: 輸出: 樣例輸入: 4
5
15 樣例輸出: 24
120
1307674368000
1 #include "cstdio" 2 #include "cstring" 3 #include "cstdlib" 4 5 int res[10010]; 6 7 int main() 8 { 9 int n; 10 while (scanf("%d", &n) == 1) 11 { 12 int i, j; 13 memset(res, 0, sizeof(res)); 14 res[0] = 1; 15 res[1] = 1; 16 for (i = 2; i <= n; i++) 17 { 18 for (j = 1; j <= res[0]; j++) 19 res[j] = res[j] * i; 20 21 for (j = 1; j <= res[0]; j++) 22 { 23 if (res[j] >= 10) 24 { 25 res[j+1] += res[j] / 10; 26 res[j] %= 10; 27 if (j == res[0]) 28 res[0]++; 29 } 30 } 31 } 32 for (i = res[0]; i >= 1; i--) 33 printf("%d", res[i]); 34 printf("\n"); 35 } 36 return 0; 37 }
?輸入一個正整數N,輸出N的階乘。
正整數N(0<=N<=1000)
?輸入可能包括多組數據,對于每一組輸入數據,輸出N的階乘
1 #include "cstdio" 2 #include "cstring" 3 #include "cstdlib" 4 5 int res[10010]; 6 7 int main() 8 { 9 int n; 10 while (scanf("%d", &n) == 1) 11 { 12 int i, j; 13 memset(res, 0, sizeof(res)); 14 res[0] = 1; 15 res[1] = 1; 16 for (i = 2; i <= n; i++) 17 { 18 for (j = 1; j <= res[0]; j++) 19 res[j] = res[j] * i; 20 21 for (j = 1; j <= res[0]; j++) 22 { 23 if (res[j] >= 10) 24 { 25 res[j+1] += res[j] / 10; 26 res[j] %= 10; 27 if (j == res[0]) 28 res[0]++; 29 } 30 } 31 } 32 for (i = res[0]; i >= 1; i--) 33 printf("%d", res[i]); 34 printf("\n"); 35 } 36 return 0; 37 }
?
轉載于:https://www.cnblogs.com/chchche/p/3466051.html
總結
以上是生活随笔為你收集整理的题目1076:N的阶乘的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET MVC3 Razor视图
- 下一篇: SharePoint 2013 Info