HDOJ-2048

神、上帝以及老天爷

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 32906    Accepted Submission(s): 13449

Problem Description

HDU 2006‘10 ACM contest的颁奖晚会隆重开始了!
为了活跃气氛,组织者举行了一个别开生面、奖品丰厚的抽奖活动,这个活动的具体要求是这样的:

首先,所有参加晚会的人员都将一张写有自己名字的字条放入抽奖箱中;
然后,待所有字条加入完毕,每人从箱中取一个字条;
最后,如果取得的字条上写的就是自己的名字,那么“恭喜你,中奖了!”

大家可以想象一下当时的气氛之热烈,毕竟中奖者的奖品是大家梦寐以求的Twins签名照呀!不过,正如所有试图设计的喜剧往往以悲剧结尾,这次抽奖活动最后竟然没有一个人中奖!

我的神、上帝以及老天爷呀,怎么会这样呢?

不过,先不要激动,现在问题来了,你能计算一下发生这种情况的概率吗?

不会算?难道你也想以悲剧结尾?!

Input

输入数据的第一行是一个整数C,表示测试实例的个数,然后是C 行数据,每行包含一个整数n(1<n<=20),表示参加抽奖的人数。

Output

对于每个测试实例,请输出发生这种情况的百分比,每个实例的输出占一行, 结果保留两位小数(四舍五入),具体格式请参照sample output。

Sample Input

1

2

Sample Output

50.00%

AC代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 const int MAX=21;
 8
 9 int main(){
10     double a[MAX];//注意一定要用浮点型,包括下面的sum和i也是
11     int i;
12     a[1]=0;
13     a[2]=1;
14     for(i=3;i<MAX;i++){
15         a[i]=(i-1)*(a[i-1]+a[i-2]);
16     }
17     int c,n;
18     while(~scanf("%d",&c)){
19         while(c--){
20             scanf("%d",&n);
21             double sum=1;//sum要定义为浮点型
22             for(double i=1;i<=n;i++){//总的可能数 (注意i要定义为浮点型)
23                 sum*=i;
24             }
25             printf("%.2lf%%\n",a[n]*100/sum);//类型后加‘%‘可实现四舍五入 (确保所有变量都为浮点型)
26         }
27     }
28     return 0;
29 }
时间: 2024-08-10 21:29:00

HDOJ-2048的相关文章

hdoj 2048 神、上帝以及老天爷(递推,全错排)

神.上帝以及老天爷 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 32717    Accepted Submission(s): 13379 Problem Description HDU 2006'10 ACM contest的颁奖晚会隆重开始了! 为了活跃气氛,组织者举行了一个别开生面.奖品丰厚的抽奖活动,这个活动的具体要求是这

HDOJ 4901 The Romantic Hero

DP....扫两遍组合起来 The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 547    Accepted Submission(s): 217 Problem Description There is an old country and the king fell in love with a

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

cocos2d-x 3.2 之 2048 —— 第四篇 ★ 核心 ★

***************************************转载请注明出处:http://blog.csdn.net/lttree****************************************** 大家十一过得肿么样啊~ 我现在的情况就是--每逢佳节 胖三斤 啊 ,胖三斤..o(╯□╰)o.. 好了,继续做2048, 这是第四篇啦~ 这篇的内容就是对触摸的处理哟~ 就是,你上下左右滑动,相应移动~ 我们先在 游戏的宏定义类 中,建立一个枚举方向变量: Game

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

HDOJ 3790 双权值Dijkstra

1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <cstring> 5 using namespace std; 6 7 const int INF = 1000000; 8 const int MAXSIZE = 1005; 9 10 int map[MAXSIZE][MAXSIZE]; 11 int price[MAXSIZE][MAXSIZE]; 1

HDOJ 1217 Floyed Template

1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <cstring> 5 #include<map> 6 using namespace std; 7 8 map<string,int>name; 9 const int INF = 1000000; 10 const int MAXSIZE = 1005; 11 const int