题目描述:
Welcome to the CCPC Qinhuangdao Site!
Qinhuangdao is a beautiful coastal city full of charm, integrating historical heritage and modern civilization. It was named after the Emperor QinShiHuang's east tour in 215 BC for seeking immortals.
The infiltration of more than 2000 years of history has left a rich cultural treasure here. Bo Yi, Shu Qi, Qi Jiguang, Cao Cao, and Mao ***, Many Heroes throughout the ages have endowed Qinhuangdao with the thousand-year cultural context, the unique and precious heritage, and the profound historical memory.
Pleasant natural scenery has shaped her beautiful appearance. Thousands of miles of Yan Mountains, the Great Wall, and the vast seas are miraculously met here. The blue sky, green land, blue sea, and golden sand gather together to welcome guests.
To toast your arrival, Alex prepared a simple problem to help you warm up.
Alex has r red balls and b blue balls. Then, Alex randomly chose two of these balls with equal probability. What is the probability that he chose two red balls?
Output the required probability in the form of irreducible fraction.
题解:
题目很长,但是问题很朴实
r个红球,b个蓝球,混在一起,随机抽两个求,抽到红球的概率
答案要写成:x/y的形式
如果答案为0就输出 0/1
如果答案是1就输出1/1
就是典型的概率问题
根据式子可以化简化简
最后还有将上下的最大公约数,保证分数为最简状态
还有记得特判情况
代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; int gcd(int x,int y) { if(y==0)return x; return gcd(y,x%y); } int main() { ios::sync_with_stdio(false); int n; cin>>n; for(int i=1;i<=n;i++){ int x,y; cin>>x>>y; int f=-1; if(x<2)f=0; if(x>=2&&y==0)f=1; if(f==0) printf("Case #%d: 0/1\n",i); else if(f==1) { printf("Case #%d: 1/1\n",i); } else { int l=x*(x-1); int r=(x+y)*(x+y-1); int z=gcd(l,r); printf("Case #%d: %d/%d\n",i,l/z,r/z); } } }