文章目录
2048、抽奖活动无人中奖的概率[错排问题]
HDU 2006’10 ACM contest的颁奖晚会隆重开始了!
为了活跃气氛,组织者举行了一个别开生面、奖品丰厚的抽奖活动,这个活动的具体要求是这样的:
首先,所有参加晚会的人员都将一张写有自己名字的字条放入抽奖箱中;
然后,待所有字条加入完毕,每人从箱中取一个字条;
最后,如果取得的字条上写的就是自己的名字,那么“恭喜你,中奖了!”
大家可以想象一下当时的气氛之热烈,毕竟中奖者的奖品是大家梦寐以求的Twins签名照呀!不过,正如所有试图设计的喜剧往往以悲剧结尾,这次抽奖活动最后竟然没有一个人中奖!
我的神、上帝以及老天爷呀,怎么会这样呢?
不过,先不要激动,现在问题来了,你能计算一下发生这种情况的概率吗?
不会算?难道你也想以悲剧结尾?!
Inupt
输入数据的第一行是一个整数C,表示测试实例的个数,然后是C 行数据,每行包含一个整数n(1<n<=20),表示参加抽奖的人数。
Output
对于每个测试实例,请输出发生这种情况的百分比,每个实例的输出占一行, 结果保留两位小数(四舍五入),具体格式请参照sample output。
Sample Input
1
2
Sample Output
50.00%
Code
//错排公式的应用--n张纸条和n个人刚刚好都不对应
//概率:错排的方式总数/n张纸条的可能的排列总数
#include<iostream>
#include<iomanip>
using namespace std;
__int64 jiechen(int n){
__int64 result=1;
for(int i=n;i>0;i--){
result=result*i;
}
return result;
}
int main(){
int t;
cin>>t;
//错排公式
__int64 a[21]={
0,0,1};
for(int i=3;i<=20;i++){
a[i]=a[i-1]*(i-1)+a[i-2]*(i-1);
}
while(t--){
int n;//人数[1,20]
cin>>n;
//计算n!
__int64 m=jiechen(n);
//概率
double x=(double)a[n]/(double)m;
cout<<setiosflags(ios::fixed);
cout<<setprecision(2)<<x*100<<"%"<<endl;
}
return 0;
}
2049、不容易系列之(4)——考新郎[错排问题]
国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样的:
首先,给每位新娘打扮得几乎一模一样,并盖上大大的红盖头随机坐成一排;
然后,让各位新郎寻找自己的新娘.每人只准找一个,并且不允许多人找一个.
最后,揭开盖头,如果找错了对象就要当众跪搓衣板…
看来做新郎也不是容易的事情…
假设一共有N对新婚夫妇,其中有M个新郎找错了新娘,求发生这种情况一共有多少种可能.
Input
输入数据的第一行是一个整数C,表示测试实例的个数,然后是C行数据,每行包含两个整数N和M(1<M<=N<=20)。
Output
对于每个测试实例,请输出一共有多少种发生这种情况的可能,每个实例的输出占一行。
Sample Input
2
2 2
3 2
Sample Output
1
3
Code
//一共有N对新婚夫妇,其中有M个新郎找错了新娘,发生这种情况一共有多少种可能.
//思路:C(N,N-M)*错排M个元素的的D(M)
#include<iostream>
using namespace std;
__int64 f(int n){
__int64 result=1;
for(int i=n;i>=1;i--){
result=result*i;
}
return result;
}
int main(){
int t;
cin>>t;
__int64 a[21]={
0,0,1};
for(int i=3;i<=20;i++){
a[i]=a[i-1]*(i-1)+a[i-2]*(i-1);
}
while(t--){
int n,m;
cin>>n>>m;//n和m的范围[1,20],n对新婚夫妇,m对找错了;
//C(n,c)的计算
int c=n-m;
__int64 b=f(n)/(f(c)*f(n-c));
cout<<b*a[m]<<endl;
}
return 0;
}
2053、Switch Game[开关灯]
There are many lamps in a line. All of them are off at first. A series of operations are carried out on these lamps. On the i-th operation, the lamps whose numbers are the multiple of i change the condition ( on to off and off to on ).
Input
Each test case contains only a number n ( 0< n<= 10^5) in a line.
Output
Output the condition of the n-th lamp after infinity operations ( 0 - off, 1 - on ).
Sample Input
1
5
Sample Output
1
0
Code
题目:有一排灯,一开始处于关闭状态,第i次对是处于i的倍数位置的的台灯进行改变其状态(由开变为关或者由关变为开),输出对其操作无穷次后第n个位置的台灯的状态(0代表关闭,1代表打开)
方法一:n的因数有m个,若m可以被2整除,则其状态为0,否则为1。因为n的因数有m个,表示其状态需要改变m次,而台灯的状态改变周期为2。
/*题目:有一排灯,一开始处于关闭状态,然后第i次对是处于i的倍数位置的的台灯进行改变其状态
(由开变为关或者由关变为开),输出对其操作无穷次后第n个位置的台灯的状态(0代表关闭,1代表打开)
思路:第n个台灯在操作n次后状态即不再改变,故而实际上是求第n次第n个台灯的状态。
思路1:找规律--除了n*n是1,其余都是0
思路2:n的因数有m个,若m可以被2整除,则其状态为0,否则为1
(对思路2的解释:n的因数有m个,表示其状态需要改变m次,而台灯的状态改变周期为2)
*/
#include<iostream>
using namespace std;
int main(){
int n;//(0,100000];
while(cin>>n){
int sum=0;
for(int i=1;i<=n;i++){
if(n%i==0){
sum++;
}
}
if(sum%2==0)
cout<<"0"<<endl;
else
cout<<"1"<<endl;
}
return 0;
}
**方法二:找规律–除了nn是1,其余都是0 ***
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n;//(0,100000];
while(cin>>n){
double a=sqrt(n);
int b=sqrt(n);
if(a==b){
cout<<"1"<<endl;
}else{
cout<<"0"<<endl;
}
}
return 0;
}
2055、An easy problem[计算n+f©]
we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, … f(Z) = 26, f(z) = -26;
Give you a letter x and a number y , you should output the result of y+f(x).
Input
On the first line, contains a number T.then T lines follow, each line is a case.each case contains a letter and a number.
Output
for each case, you should the result of y+f(x) on a line.
Sample Input
6
R 1
P 2
G 3
r 1
p 2
g 3
Sample Output
19
18
10
-17
-14
-4
Code:
方法一:暴力
//按要求计算
#include<iostream>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
char c;
int n;
cin>>c>>n;
//计算n+f(c)
switch(c){
case 'A':
cout<<1+n<<endl;
break;
case 'a':
cout<<n-1<<endl;