uva 10288 Coupons (分数模板)
https://vjudge.net/problem/UVA-10288
大街上到处在卖彩票,一元钱一张。购买撕开它上面的锡箔,你会看到一个漂亮的图案。
图案有n种,如果你收集到所有n(n≤33)种彩票,就可以得大奖。
请问,在平均情况下,需要买多少张彩票才能得到大奖呢?
答案以带分数形式输出
例:当n=5时

思路简单,就是输出麻烦
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=;
struct fraction
{
long long numerator;
long long denominator;
fraction()
{
numerator=;
denominator=;
}
fraction(long long num)
{
numerator=num;
denominator=;
}
fraction(long long a,long long b)
{
numerator=a;
denominator=b;
this->reduction();
}
void operator =(const long long num)
{
numerator=num;
denominator=;
this->reduction();
}
void operator=(const fraction b)
{
numerator=b.numerator;
denominator=b.denominator;
this->reduction();
}
fraction operator + (const fraction b)const
{
long long gcdnum=__gcd(denominator,b.denominator);
fraction tmp=fraction(numerator*(b.denominator/gcdnum)+b.numerator*(denominator/gcdnum),denominator/gcdnum*b.denominator);
tmp.reduction();
return tmp;
}
fraction operator + (const int b)const
{
return ((*this)+fraction(b));
}
fraction operator - (const fraction b)const
{
return ((*this)+fraction(-b.numerator,b.denominator));
}
fraction operator - (const int b)const
{
return ((*this)-fraction(b));
}
fraction operator * (const fraction b)const
{
fraction tmp=fraction(numerator*b.numerator,denominator*b.denominator);
tmp.reduction();
return tmp;
}
fraction operator * (const int b)const
{
return ((*this)*fraction(b));
}
fraction operator / (const fraction b)const
{
return ((*this)*fraction(b.denominator,b.numerator));
}
void reduction()
{
if(numerator==)
{
denominator=;
return;
}
long long gcdnum=__gcd(numerator,denominator);
numerator/=gcdnum;
denominator/=gcdnum;
}
void print()
{
if(denominator==) { printf("%lld\n",numerator); }
else
{
long long num=numerator/denominator;
long long tmp=num;
int len=;
while(tmp)
{
len++;
tmp/=;
}
for(int i=;i<=len;i++) printf(" ");
if(len) printf(" ");
printf("%lld\n",numerator%denominator);
if(num) printf("%lld ",num);
tmp=denominator;
while(tmp)
{
printf("-"); tmp/=;
}
puts("");
for(int i=;i<=len;i++) printf(" ");
if(len) printf(" ");
printf("%lld\n",denominator);
}
}
}f[maxn];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
f[]=;
for(int i=;i<=n;i++)
f[i]=f[i-]+fraction(,i);
f[n]=f[n]*n;
f[n].print();
}
return ;
}
uva 10288 Coupons (分数模板)的更多相关文章
- UVA 10288 - Coupons(概率递推)
UVA 10288 - Coupons option=com_onlinejudge&Itemid=8&page=show_problem&category=482&p ...
- Uva 10288 Coupons
Description Coupons in cereal boxes are numbered \(1\) to \(n\), and a set of one of each is require ...
- UVa 10288 - Coupons(数学期望 + 递推)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 10288 Coupons 彩票 (数学期望)
题意:一种刮刮卡一共有n种图案,每张可刮出一个图案,收集n种就有奖,问平均情况下买多少张才能中奖?用最简的分数形式表示答案.n<=33. 思路:这题实在好人,n<=33.用longlong ...
- UVA 10288 Coupons (概率)
题意:有n种纸片无限张,随机抽取,问平均情况下抽多少张可以保证抽中所有类型的纸片 题解:假设自己手上有k张,抽中已经抽过的概率为 s=k/n:那抽中下一张没被抽过的纸片概率为 (再抽一张中,两张中,三 ...
- GCD LCM 最大公约数 最小公倍数 分数模板 (防溢出优化完成)
自己写的一个分数模板,在运算操作时进行了防溢出的优化: ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } ll lcm(ll a, ll b) { ...
- UVa10288 Coupons 分数模版
UVa10288 题目非常简单, 答案就是 n/n+n/(n-1)+...+n/1; 要求分数输出.套用分数模板.. #include <cstdio> #include <cstr ...
- UVA 10288 Coupons---概率 && 分数类模板
题目链接: https://cn.vjudge.net/problem/UVA-10288 题目大意: 一种刮刮卡一共有n种图案,每张可刮出一个图案,收集n种就有奖,问平均情况下买多少张才能中奖?用最 ...
- UVa 10288 (期望) Coupons
题意: 每张彩票上印有一张图案,要集齐n个不同的图案才能获奖.输入n,求要获奖购买彩票张数的期望(假设获得每个图案的概率相同). 分析: 假设现在已经有k种图案,令s = k/n,得到一个新图案需要t ...
随机推荐
- SSD/Memory技术学习拼图
"打酱油"解读SLC缓存新技术[http://www.pceva.com.cn/article/3612-1.html] 固态硬盘主控将消亡?未来高性能固态硬盘长这样[http:/ ...
- unrecognized selector send to instancd 快速定位
1.在Debug菜单中Breakpoints->Create Symbolic Breakpoint; 2.在Symbolic中填写方法签名: -[NSObject(NSObject) does ...
- NSURLErrorDomain错误代码
kCFURLErrorUnknown = -998, kCFURLErrorCancelled = -999, kCFURLErrorBadURL = -1000, kCFURLErrorTimedO ...
- 如何通过JAVA让DB2调用操作系统命令
引言:我们在工作中常用操作系统命令和DB2命令对数据库做数据的导入.导出等操作,但是DB2不支持复合SQL 语句调用操作系统命令,因此我们需要利用 UDF 来执行SQL 中不可用的操作(例如:执行一些 ...
- iOS- 关于AVAudioSession的使用——后台播放音乐
1.前言 •AVAudioSession是一个单例,无需实例化即可直接使用.AVAudioSession在各种音频环境中起着非常重要的作用 •针对不同的音频应用场景,需要设置不同的音频会话分类 1 ...
- Struts2(六)
以下内容是基于导入struts2-2.3.32.jar包来讲的 1.OGNL OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表 ...
- PHP 多文件打包下载 zip
<?php $zipname = './photo.zip'; //服务器根目录下有文件夹public,其中包含三个文件img1.jpg, img2.jpg, img3.jpg,将这三个文件打包 ...
- 第132天:移动web端-rem布局(进阶)
rem布局(进阶版) 该方案使用相当简单,把下面这段已压缩过的 原生JS(仅1kb,源码已在文章底部更新,2017/5/3) 放到 HTML 的 head 标签中即可(注:不要手动设置viewport ...
- HttpHandler与HttpModule的理解与应用
问题1:什么是HttpHandler? 问题2:什么是HttpModule? 问题3:什么时候应该使用HttpHandler什么时候使用HttpModule? 答案1:HttpHandler,Http ...
- BZOJ 1226 学校食堂(状压DP)
状压DP f(i,j,k)表示前i−1个人已经吃了饭,且在i之后的状态为j的人也吃了饭(用二进制表示后面的状态),最后吃的那个人是i之后的第k个 (注意k可以是负数) 然后 如果j&1=1那么 ...