这是我第一次写博客,作为一个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. Java中的Clone机制(浅层复制)

    浅层复制代码: import java.util.*; class Int{ private int i; public Int(int ii){i = ii;} public void increm ...

  2. No1_6.字符串的基本操作2_Java学习笔记

    import java.util.Scanner; import java.util.regex.Pattern; public class HelloString2 { public static ...

  3. django post方法不能提交

    def login(request): if request.method == 'GET': c = {} c.update(csrf(request)) return render_to_resp ...

  4. DataTables选择行并删除(删除单行)

    $(document).ready(function() { var table = $('#example').DataTable(); $('#example tbody').on( 'click ...

  5. linux源码安装nodejs

    如何在linux上安装nodejs环境 到网下载安装文件node-v5.10.1-linux-x64.tar.gz.   将源码包上传到linux的/usr/local/node/目录下,在该目录下解 ...

  6. 迁移笔记:php截取文字的方法

    php内置函数 1. iconv iconv_set_encoding('internal_encoding', 'UTF-8'); $str; //字符串的声明 $num=iconv_strlen( ...

  7. ZOJ 3188 ZOJ 3188 Treeland Exhibition(树形DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3278 题意:给出一棵树,找出一个不大于长度为m的链,使得其他点到该链 ...

  8. 《Programming WPF》翻译 第7章 3.笔刷和钢笔

    原文:<Programming WPF>翻译 第7章 3.笔刷和钢笔 为了在屏幕上绘制一个图形,WPF需要知道你想要为图形填充什么颜色以及如何绘制它的边框.WPF提供了一些Brush类型支 ...

  9. C/C++ 指针的非空判断

    一定要分得清楚C和C++的“空指针常量”不是一样的.C标准不保证NULL等于0,所以做指针非空判断时,应该用if(p != NULL):因为“上下文转换到bool值”的统一性,C++就应该用if(p) ...

  10. Eclipse标准版安装J2EE

    虽然有Eclipse IDE for Java EE Developers,已经包含了j2ee的插件,但有时我们需要在标准版上安装插件来达到开发j2ee的功能. 安装 Java EE 插件:  * 依 ...