标签:online judge 九度 编程 c++
输入一个正整数N,输出N的阶乘。
正整数N(0<=N<=1000)
输入可能包括多组数据,对于每一组输入数据,输出N的阶乘
4 5 15
24 120 1307674368000
#include <iostream> #include <string.h> #include <stdio.h> //不超时的代码 using namespace std; int main() { int N; int num[3001]; while(cin >>N) { int i,j,k; memset(num,0,sizeof(num)); num[0] = 1; int length = 1; if(N == 0) { cout << 1 <<endl; continue; } for(i = 1; i<=N ; i++) { int over = 0; for(j = 0; j < length; j++) { num[j] = num[j] * i + over; if(num[j] >= 10) { over = num[j] / 10; num[j] = num[j] % 10; } else over = 0; } while(over != 0) { int temp = over % 10; num[length ++] = temp; over = over / 10; } } for(i = 3000;i >=0 ; i--) if(num[i] != 0) break; for(j = i ; j >= 0 ; j--) cout<<num[j]; cout << endl; } return 0; } /************************************************************** Problem: 1076 User: Carvin Language: C++ Result: Accepted Time:1320 ms Memory:1520 kb ****************************************************************/
标签:online judge 九度 编程 c++
原文地址:http://blog.csdn.net/carvin_zh/article/details/45028519