hdu-2048-神、上帝以及老天爷
2014-07-29 16:05
323 查看
解题思路:
典型的错排题目
首先求出错排的情况,然后求出错排的所有情况,以前者除以后者就是百分比
现在求对应的错排情况。容易知道,f(1)=0,f(2)=1
然后,对于N个人,有n-1个人拿错票和n-2个人拿错票的情况
当有n-1个拿错票时,在加入一个人,只要第n个人和前面任意的n-1个人其中一个调换票就可以了,所以有f(n-1)*(n-1)。当有n-2个拿错票时,只能是没拿错的那个人与第n个交换票,而那个人可能是前面n-1个的任意一个,所以又有f(n-2)*(n-1)
所以错排结果为(n-1)*(f(n-1)+f(n-2))。
代码:
//法一
#include<stdio.h>
__int64 F(int n)
{
if(n==1) return 0;
else if(n==2) return 1;
else return (n-1)*(F(n-1)+F(n-2));
}
int main()
{
int s,n,i;
scanf("%d",&s);
while(s--)
{
__int64 m=1;
double sum;
scanf("%d",&n);
for(i=2;i<=n;i++)
m*=i;
sum=F(n)*100.0/m;
printf("%.2lf%%\n",sum);
}
return 0;
}
//法二
#include<iostream>
using namespace std;
int main()
{
int s,n;
cin>>s;
while(s--)
{
cin>>n;
if(n==2) cout<<"50.00%"<<endl;
else if(n==3) cout<<"33.33%"<<endl;
else if(n==4) cout<<"37.50%"<<endl;
else if(n==5) cout<<"36.67%"<<endl;
else if(n==6) cout<<"36.81%"<<endl;
else cout<<"36.79%"<<endl;
}
return 0;
}
典型的错排题目
首先求出错排的情况,然后求出错排的所有情况,以前者除以后者就是百分比
现在求对应的错排情况。容易知道,f(1)=0,f(2)=1
然后,对于N个人,有n-1个人拿错票和n-2个人拿错票的情况
当有n-1个拿错票时,在加入一个人,只要第n个人和前面任意的n-1个人其中一个调换票就可以了,所以有f(n-1)*(n-1)。当有n-2个拿错票时,只能是没拿错的那个人与第n个交换票,而那个人可能是前面n-1个的任意一个,所以又有f(n-2)*(n-1)
所以错排结果为(n-1)*(f(n-1)+f(n-2))。
代码:
//法一
#include<stdio.h>
__int64 F(int n)
{
if(n==1) return 0;
else if(n==2) return 1;
else return (n-1)*(F(n-1)+F(n-2));
}
int main()
{
int s,n,i;
scanf("%d",&s);
while(s--)
{
__int64 m=1;
double sum;
scanf("%d",&n);
for(i=2;i<=n;i++)
m*=i;
sum=F(n)*100.0/m;
printf("%.2lf%%\n",sum);
}
return 0;
}
//法二
#include<iostream>
using namespace std;
int main()
{
int s,n;
cin>>s;
while(s--)
{
cin>>n;
if(n==2) cout<<"50.00%"<<endl;
else if(n==3) cout<<"33.33%"<<endl;
else if(n==4) cout<<"37.50%"<<endl;
else if(n==5) cout<<"36.67%"<<endl;
else if(n==6) cout<<"36.81%"<<endl;
else cout<<"36.79%"<<endl;
}
return 0;
}
相关文章推荐
- HDU 2048 神、上帝以及老天爷
- HDU 2048 神、上帝以及老天爷
- HDU 2048 - 神、上帝以及老天爷
- HDU-2048 神、上帝以及老天爷
- HDU 2048(神、上帝以及老天爷)——错排,DP,递推
- [ACM] hdu 2048 神、上帝以及老天爷 (错位排列公式)
- HDU 2048 神、上帝以及老天爷
- hdu 2048 神、上帝以及老天爷
- hdu 2048 神、上帝以及老天爷
- hdu 2048 神、上帝以及老天爷(水题,递推,错排)
- hdu 2048 神、上帝以及老天爷
- [hdu-2048] 神、上帝以及老天爷
- hdu 2048 神、上帝以及老天爷
- HDU 2048 神、上帝以及老天爷 (递推&错排概率)
- HDU 2048 神、上帝以及老天爷
- HDU 2048 神、上帝以及老天爷
- hdu 2048 神、上帝以及老天爷 解题报告
- HDU-2048神、上帝以及老天爷
- hdu 2048 神、上帝以及老天爷
- hdu 2048 神、上帝以及老天爷(错排)