题意:
给你n个红球和m个蓝色球。然后以相等的概率随机选择了其中两个。选择两个红球的概率是多少?
题目:
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 Zedong, 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 red balls and 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.
Input
The first line of input gives the number of test cases, . test cases follow.
For each test case, the only line contains two integers , where is the number of red balls and is the number of blue balls.
Output
For each test case, output one line containing “Case #x: y”, where is the test case number (starting from ), and is the answer in the form of irreducible fraction in format A/B.
If the required probability equals to zero, output 0/1. If the required probability equals to , output 1/1.
Example
Input
3
1 1
2 1
8 8
Output
Case #1: 0/1
Case #2: 1/3
Case #3: 7/30
分析:
1.注意审题,在红球数量小于2时,既不可能存在取两个红球的情况下直接输出0/1;
2.欧几里得算法。
AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int t,n,m,k,a,b;
int gcd(int a,int b)
{return b==0?a:gcd(b,a%b);
}
int main()
{k=1;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);printf("Case #%d: ",k++);if(n<2)printf("0/1\n");else{a=n*(n-1);b=(n+m)*(n+m-1);printf("%d/%d\n",a/gcd(a,b),b/gcd(a,b));}}return 0;
}