【机试备考】Day28-分数加法 | 水题

题目

BUPT 2014 网研 ProblemA(oj)
求2^-a + 2^-b,其中a和b均为正整数,结果请用最简分数表示。

输入描述

第一行为测试数据的组数T(1<=T<=400)。请注意,任意两组测试数据之间是相互独立的。
每组测试数据一行,包含两个整数a和b(2<=a,b<=20)。

输出描述

对于每组测试数据,在一行内输出结果,分子和分母用“/”隔开。

示例

输入

2
2 4
3 2

输出

5/16
3/8

题解

#include <bits/stdc++.h>
using namespace std;
int main(){
    
    
	int t;
	cin>>t;
	for(int i=1;i<=t;i++)
    {
    
    
        int a,b;
        cin>>a>>b;
        //一开始忽略了
        if(a==b)
        {
    
    
            cout<<"1/"<<pow(2,a-1)<<endl;
            continue;
        }
        int big=a>b?a:b;
        int small=a<b?a:b;
        int mom=pow(2,big);//分母
        int son=1+pow(2,(big-small));//分子
        cout<<son<<"/"<<mom<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43417265/article/details/114180046