排排队
Time Limit:1000MS Memory Limit:65536K
Total Submit:31 Accepted:29
Description
有一列人,按1到n的顺序对这n个人进行编号。现在这n个人开始排队,有一个排队规则
就是编号为i的人不能站在第i位上,问:n个人进行排队,有多少种可能的排队方法?
Input
包括多组测试数据,每组测试数据包含一个整数n,代表一共有n个人进行排队
Output
包括一个整数,代表多少种可能的排队方法。
Sample Input
3
4
6
Sample Output
2
9
265
Source
#include<stdio.h>
#include<math.h>
int fa(int n) //自定义factorial函数
{
int f=1;
while(n)
{
f*=n--;
}
return f;
}
int main()
{
int n,i,d=0;
while(scanf("%d",&n)!=EOF) //因为输入多组数据 ,注意这里的控制
{
d=0;
if(n==1){d=0;}
else
{
for(i=2;i<=n;i++)
{
d+=fa(n)*pow(-1,i)/fa(i);//靠着高中的记忆,隐约记着这个公式.d=n!(1/2!-1/3!+...(-1)^n/n!)
}
}
printf("%d\n",d);
}
return 0;
}