这是我第一次写博客,作为一个ACMer,经常进别人的博客,所以自己也想写写博客。

HDU 1099

Lottery

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

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到n共n种,张数不限,问你平均买多少张能把编号为1到n的n中彩票全买下来,也就是求期望。
举个例子,当n=5时,第一张有用的概率为1,买一张就行了,第二张有用的概率为4/5,所以买5/4张彩票能买上对你有用的,一次类推,sum=1+5/4+5/3+5/2+5/1=11…5/12,再有就是注意格式
附上我的AC代码

#include <iostream>
using namespace std; int n,s,a1,b1,a2,b2,s1,s2;
int gcd(int x,int y)
{
int t;
while (x%y!=0)
{
t=x%y;
x=y;
y=t;
}
return y;
}
int f(int x,int y)
{
int t1=a1,t2=b1;
a1=t1*y+b1*x;
b1=t2*y;
int t=a1/b1;
s+=t;
a1-=t*b1;
t=gcd(a1,b1);
a1=a1/t;
b1=b1/t;
}
int main()
{
while (cin>>n)
{
s=0;
a1=0;b1=1;
for (int i=1;i<=n;i++)
{
f(n,i);
}
if (a1==0)
cout <<s<<endl;//" "<<a1<<" "<<b1<<endl;
else
{
int t1=0,t2=0,temp1=s,temp2=b1;
while (temp1!=0)
{
t1++;
temp1/=10;
}
t1++;
while (temp2!=0)
{
t2++;
temp2/=10;
}
for (int i=1;i<=t1;i++)
cout <<" ";
cout <<a1<<endl;
cout <<s<<" ";
for (int i=t2;i>=1;i--)
cout <<"-";
cout <<endl;
for (int i=1;i<=t1;i++)
cout <<" ";
cout <<b1<<endl;
}
}
return 0;
}

hdu 1099 Lottery的更多相关文章

  1. HDU 1099 Lottery (求数学期望)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1099 Lottery Time Limit: 2000/1000 MS (Java/Others)   ...

  2. HDU - 1099 - Lottery - 概率dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1099 最最简单的概率dp,完全是等概率转移. 设dp[i]为已有i张票,还需要抽几次才能集齐的期望. 那么dp[ ...

  3. 1099 Lottery

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1099 英文水平太差了,题目实在是不知道是什么意思,然后看了其他高手写的思路,才看明白. 题意,收集n张彩票 ...

  4. HDUOJ 1099——Lottery

    Lottery Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  5. hdu 1099(数学)

    Lottery Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  6. 【HDOJ】1099 Lottery

    题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. #include <cstdio ...

  7. 2018年暑假ACM个人训练题7 题解报告

    A:HDU 1060 Leftmost Digit(求N^N的第一位数字 log10的巧妙使用) B:(还需要研究一下.....) C:HDU 1071 The area(求三个点确定的抛物线的面积, ...

  8. HDU1099---数学 | 思维

    hdu 1099 Lottery题意:1~n编号的彩票,要买全,等概率条件下平均要买几张.已经买了m张时,买中剩下的概率为1-m/n,则要买的张数为1/(1-m/n)n=2,s=1+1/(1-1/2) ...

  9. HDU 2985 Another lottery(坑题)

    点我看题目 题意 : 有n个人,每个人可以玩m轮,每一轮中每个参与者都有cj张票,第 i 轮的奖金是2的i次方,问你每个人所能赢得最多的奖金的概率是多少. 思路 : 这个题比较坑啊,其实不用去算前几轮 ...

随机推荐

  1. Protobuf, understand the hood

    proto文件定义 package lm; message Foo{ required int32 id = 1; } message Bar{ required int32 id = 1 [defa ...

  2. 【android】android中activity的启动模式

    在AndroidManifest.xml中配置 <activity android:label="第二个应用" android:name=".Demo2Activi ...

  3. MyEclipse java was started but returned exit code=-1

    java was started but returned exit code=-1 Vm指的是java虚拟机,默认你安装MyEclipse时会自带一个java虚拟机,Vm配置那一行换成你安装的jav ...

  4. Ajax调用asp.net后台代码

    后台代码: [WebMethod] public static string CreateDate(string name,string age) { return "姓名:"+n ...

  5. -webkit-appearance: none;

    今天在web群里聊天的时候,发现了这个东东,我好像不怎么认识他,于是查了下关于他的信息: 抄的例子, ----------- IOS环境下的按钮都是经过美化的,但通常我们在设计web app的时候不需 ...

  6. js获取IP地址的方法小结

    s代码获取IP地址的三种方法,在js中取得客户端的IP地址. 原文地址:http://www.jbxue.com/article/11338.html 1,js取得IP地址的方法一 <scrip ...

  7. AngularJS中的控制器示例_2

    <!doctype html> <html ng-app="myApp"> <head> <script src="C:\\Us ...

  8. Effective Java2读书笔记-对于所有对象都通用的方法(一)

    第8条:覆盖equals时请遵守通用约定 ①约定的内容 自反性.对于任何非null的引用值x.x.equals(x)必须返回true. 对称性.对于任何非null的引用值x和y.当且仅当y.equal ...

  9. Keil C51内存分配与优化

    C51的内存分配不同于一般的PC,内存空间有限,采用覆盖和共享技术.在Keil编译器中,经过编译后,会形成一个M51文件,在其内部可以详细的看到内存的分配情况. C51内存常见的两个误区: A.变量超 ...

  10. WPF笔记(2.8 常用的布局属性)——Layout

    原文:WPF笔记(2.8 常用的布局属性)--Layout 这一节老没意思,啰里啰唆的尽是些HTML的属性,挑几个好玩的List出来,备忘:Padding与Margin的区别:Margin指控件边界与 ...