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 ...
随机推荐
- Unicode 和 UTF-8 有何区别
作者:于洋链接:https://www.zhihu.com/question/23374078/answer/69732605来源:知乎著作权归作者所有,转载请联系作者获得授权. ========== ...
- HDU 2012 FZU 1756关于素数的一些水题
HDU 2012 素数判定 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 按照事务类型分析 DB2 事物的性能
概述 事务是数据库系统中的核心概念之一.作为数据库系统的逻辑工作单元(Unit of Work),事务必须具有四个属性,即原子性.一致性.隔离性和持久性(ACID).数据库系统往往通过锁机制保证事务的 ...
- 软工实践团队展示——WorldElite
软工实践团队展示--WorldElite 本次作业链接 团队成员 031602636许舒玲(组长) 031602237吴杰婷 031602634吴志鸿 081600107傅滨 031602220雷博浩 ...
- 【Biocode】产生三行的seq+01序列
代码说明: sequence.txt与site.txt整合 如下图: sequence.txt: site.txt: 整理之后如下: 蛋白质序列中发生翻译后修饰的位置标记为“1”,其他的位置标记为“0 ...
- FreeMarker(XML模板)导出word
在项目中使用它完成的功能是按照固定的模板将数据导出到Word.比如台账.在完成后将处理过程按照台账的要求导出,有时程序中需要实现生成标准Word文档,要求能够打印,并且保持页面样式不变. 这个功能就是 ...
- [LeetCode] MaximumDepth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- python的N个小功能(文件内容的匹配替换)
# -*- coding: utf-8 -*- """ Created on Fri Feb 17 20:25:05 2017 @author: who "&q ...
- 打印实例对象的名字 默认调用父类的toString 可重写
- 【hdu5306】Gorgeous Sequence 线段树区间最值操作
题目描述 给你一个序列,支持三种操作: $0\ x\ y\ t$ :将 $[x,y]$ 内大于 $t$ 的数变为 $t$ :$1\ x\ y$ :求 $[x,y]$ 内所有数的最大值:$2\ x\ y ...