HDU 1099 Lottery 乐透彩
2015-07-26 20:20
281 查看
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1099
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2926 Accepted Submission(s): 1309
Problem Description
Eddy’s company publishes a kind of lottery.This set of lottery which are numbered 1 to n, and a set of one of each is required for a prize .With one number per lottery, how many lottery on average are required to make a complete set of n coupons?
Input
Input consists of a sequence of lines each containing a single positive integer n, 1<=n<=22, giving the size of the set of coupons.
Output
For each input line, output the average number of lottery required to collect the complete set of n coupons. If the answer is an integer number, output the number. If the answer is not integer, then output the integer part of the answer followed by a space and then by the proper fraction in the format shown below. The fractional part should be irreducible. There should be no trailing spaces in any line of ouput.
Sample Input
2
5
17
Sample Output
3
5
11 –
12
340463
58 ——
720720
注意公式:买中的概率*买的张数=1。
假如发行1张,买1次就集齐了。所以买1张。
假如发行2张,第一次买的序号是1,第二次买中剩下那张的概率是1/2,所以要买两张才能买到第二张,所以要买3张才能才能集齐。
假如发行3张,第一次发的序号是1,要买1张,第二次买中剩下的两张之一的概率是2/3,所以要买3/2张,第三次买剩中最后一张的概率是1/3,所以要买3张,所以要买5+1/2张。
假如发行n张,第一次买中没买过的概率是1,第二次是n-1/n,第三次是n-2/n,第n次是1/n,
而对应需要买的张数是第一次买1张,第二次买n/n-1张,第三次买n/n-2,第n次买n张,所以求的是n/n,n/n-1,……1/n的和。
最后注意输出的结果是约分过的带分数,注意横线是减号。
题目:
LotteryTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2926 Accepted Submission(s): 1309
Problem Description
Eddy’s company publishes a kind of lottery.This set of lottery which are numbered 1 to n, and a set of one of each is required for a prize .With one number per lottery, how many lottery on average are required to make a complete set of n coupons?
Input
Input consists of a sequence of lines each containing a single positive integer n, 1<=n<=22, giving the size of the set of coupons.
Output
For each input line, output the average number of lottery required to collect the complete set of n coupons. If the answer is an integer number, output the number. If the answer is not integer, then output the integer part of the answer followed by a space and then by the proper fraction in the format shown below. The fractional part should be irreducible. There should be no trailing spaces in any line of ouput.
Sample Input
2
5
17
Sample Output
3
5
11 –
12
340463
58 ——
720720
思路:
每次发行n张彩票,要买多少张才能集齐。注意公式:买中的概率*买的张数=1。
假如发行1张,买1次就集齐了。所以买1张。
假如发行2张,第一次买的序号是1,第二次买中剩下那张的概率是1/2,所以要买两张才能买到第二张,所以要买3张才能才能集齐。
假如发行3张,第一次发的序号是1,要买1张,第二次买中剩下的两张之一的概率是2/3,所以要买3/2张,第三次买剩中最后一张的概率是1/3,所以要买3张,所以要买5+1/2张。
假如发行n张,第一次买中没买过的概率是1,第二次是n-1/n,第三次是n-2/n,第n次是1/n,
而对应需要买的张数是第一次买1张,第二次买n/n-1张,第三次买n/n-2,第n次买n张,所以求的是n/n,n/n-1,……1/n的和。
最后注意输出的结果是约分过的带分数,注意横线是减号。
代码:
#include <iostream> #include"string.h" #include"cstdio" #include"stdlib.h" #include"algorithm" using namespace std; typedef long long int lint; typedef unsigned long long int ulint; ulint gcs(ulint a,ulint b) { ulint x=a; ulint y=b; ulint z; while(y!=0) { z=x%y; x=y; y=z; } return x; } int main() { int x; while(scanf("%d",&x)!=EOF) { ulint fenmu=1; ulint fenzi=1; ulint tm; ulint tz; for(int i=2; i<=x; i++) { tm=fenmu*i; tz=fenzi*i+fenmu; ulint g=gcs(tm,tz); fenmu=tm/g; fenzi=tz/g; } fenzi=fenzi*x; ulint g=gcs(fenmu,fenzi); fenmu=fenmu/g; fenzi=fenzi/g; ulint zs=fenzi/fenmu; ulint fs=fenzi%fenmu; if(fs==0) { printf("%I64u\n",zs); } else { int l1=0; int t=zs; while(t>0) { t=t/10; l1++; } int l2=0; t=fenmu; while(t>0) { t=t/10; l2++; } for(int i=0;i<=l1;i++) { printf(" "); } printf("%I64u\n",fs); printf("%I64u ",zs); for(int i=0;i<l2;i++) { printf("-"); } printf("\n"); for(int i=0;i<=l1;i++) { printf(" "); } printf("%I64u\n",fenmu); } } return 0; }
相关文章推荐
- Codevs3945 完美拓印
- 长度单位与内外边距
- windows 控制面板
- 将十进制数转换为二进制数
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
- [Leetcode 142] Linked List Cycle II
- [LeetCode]Binary Tree Right Side View
- 按键事件在java framework中的流程
- Head First设计模式悟道
- 常用的汉字,用于获得随机图片
- HDU 5310 Souvenir
- 找短板1:柱状图中的最大矩阵面积
- HDU 2222 Keywords Search(AC自动机)
- c语言 模拟 库函数 strstr()函数
- 埃氏筛法(快速筛选n以内素数的个数)
- UVA 299 Train Swapping
- hdu 5306 Gorgeous Sequence(暴力线段树)(好题)
- Loadrunner关于页面检查的几个函数详解
- tomcat启动成功外网无法访问
- 求二叉树中节点的最大距离