hdu 1099(数学)
2016-06-08 22:20
260 查看
Lottery
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3333 Accepted Submission(s): 1489
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
5
17
[align=left]Sample Output[/align]
3
5
11 --
12
340463
58 ------
720720
[align=left]Author[/align]
eddy
题意很难理解。。看了别人的翻译才懂。。
题目的大概意思是说一套彩票有编号1到n共n种,张数不限,问你平均买多少张能把编号为1到n的n中彩票全买下来,也就是求期望。
也就是求n/n+n/(n-1)+...+n/1..迭代求解 a/b+1/i = ai+b/(bi)
第一组测试用例后面明明有一个空格...结果加了之后还报了一次格式错误。。。
#include<cstdio> #include<cstring> #include<algorithm> #include<math.h> using namespace std; typedef long long LL; LL gcd(LL a,LL b){ return b==0?a:gcd(b,a%b); } LL lcm(LL a,LL b){ return a/gcd(a,b)*b; } int getLen(LL num){ int ans = 0; while(num){ ans++; num/=10; } return ans; } int main() { int n; while(scanf("%d",&n)!=EOF){ LL a=1,b=1; ///a为分子,b为分母 for(int i=2;i<=n;i++){ a = a*i+b; b = b*i; LL d = gcd(a,b); a/=d; b/=d; } a=a*n; LL d = gcd(a,b); a/=d,b/=d; LL res = a/b; if(a%b==0){ printf("%lld\n",a/b); }else { LL len = getLen(res); LL len1 = getLen(b); LL yushu = a%b; for(int i=0;i<=len;i++){ printf(" "); } printf("%lld\n%lld ",yushu,res); for(int i=0;i<len1;i++) printf("-"); printf("\n"); for(int i=0;i<=len;i++){ printf(" "); } printf("%lld\n",b); } } return 0; }
相关文章推荐
- IO流中将字节流转成字符流的方法
- 第二阶段团队项目冲刺第十天
- 匿名页的反向映射
- LeetCode--No.136--Single Number
- Python学习笔记(三)windows下安装theano
- UITextField、UITextView、UIWebViewd
- StringcbCopyN
- 在Mysql中如何显示所有用户?
- HDU 5058(set) 5059 (字符串操作)
- leetcode-Reorder List-143
- CentOS 7安装Splunk
- StarUML 破解方法
- 使用NtUserBuildHwndList遍历窗口_win7x64
- python模块使用
- GDB踪函数的完整调用过程 及原理
- 求字符串的所有组合
- Javascript笔记整理 —— 提升
- Resource概述
- BZOJ·人生相关
- JS寻找宝藏游戏