您的位置:首页 > 其它

1003 -- 计算N!

2015-08-29 03:13 351 查看
计算N!
Time Limit:1000MS Memory Limit:65536K

Total Submit:931 Accepted:209
Description
Description:

你的好友小王最近学了一个新的运算法则——阶乘,但他很懒,不想一步一步计算,早知道你是一个程序高手,所以他想让你来帮他编一个程序,能马上得到N的阶乘。

Input
Input:

输入包含若干行数据,每行都有一个整数N(0<=N<=500)。

Output
Output:

与输入相对应每行输出N的阶乘。

Sample Input
Sample Input:
2
4

Sample Output
Sample Output:
2
24

Source
ahstu@ICPC01

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AK1003 {
/// <summary>
/// 大数阶乘
/// 本代码参考老赵以前写过的大数阶乘代码
/// </summary>
class Program {
static void Main(string[] args) {
string sb;
int i, j, temp, start, sc, N = 1200;
while ((sb = Console.ReadLine()) != null) {
int n = int.Parse(sb);
int[] a = new int
;
for (a[0] = 1, i = 1; i <= n; i++)
for (sc = 0, j = 0; j < N; j++) {
temp = a[j] * i + sc;//当前这一位的数乘上i,然后加上后面来的进位
sc = temp / 10;//sc代表进位
a[j] = temp % 10;//本位保留模后的结果
}
//数据是倒着存的,即1*2*3*4=24,存是是按420000000...保存的,一直有N=1200位
for (start = N - 1; a[start] == 0; --start) ;//这里是确定从后面哪一位开始输出,从后向前输出
for (; start >= 0; --start)
Console.Write(a[start]);
Console.WriteLine();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: