在线等 C语言高手请进!

来源:百度知道 编辑:UC知道 时间:2024/05/28 14:37:59
Description

UHA的要开运动会了,学校为入场式上的A方阵准备了道具。为了突出多样性,每个方阵成员将拿不同的道具入场。已知方阵由n(1<=n<=20)个人组成,学校恰好也准备了n个互不相同的道具。为了研究这些道具共有多少种分配方案,学校找到了你这个“计算机专家”。

Input

输入包括多组数据。
每行包括1个数据:n。
n是整数,且1<=n<=20。
处理到文件结束为止。

Output

每组数据的输出都只有一行,为分配方案的种数。

Sample Input

1
2
3

Sample Output

1
2
6

这题转换成最后就是求n!
难点在于20!一般变量放不下,需要用别的方法

#include <stdio.h>
int main()
{
int n = 1;
int a[2000];
int carry;
int i;
int digit = 1;
a[0] = 1;
int temp;
scanf("%d", &n);
for(i = 2; i <= n; ++i)
{
carry = 0;
for(int j = 1; j <= digit; ++j)
{
temp = a[j-1] * i + carry;
a[j-1] = temp % 10;
carry = temp / 10;
}
while(carry)
{
a[++digit-1] = carry % 10;
carry /= 10;
}
}
for(i = digit; i >=1; --i)
{
printf("%d",a[i-1]);
}
printf("\n");
n++;
return 0;
}

关键算法

先打表,提高效率,输出结果应该是n!

int[21] a;

a[1] = 1;

for(int i=2;i<21;i++)
a[i] = i *a[i-1];

a[n]就是n!了

while(读取一个数n)
{
输出a[n];
}
至于while(读取一个数n)的实现