天天看點

【北郵OJ】266. 分數加法-網研14

總結:

1。注意int資料溢出,此題2^a有可能非常大,幹脆用long long int

2。gcd()遞歸

#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;

long long int gcd(long long int a,long long int b){
    if(b == ) {return a;}
    else {return gcd(b,a%b);}
}

int main(){
    int t,T;
    cin>>T;
    for(t=;t<=T;t++){
        long long int a,b;
        long long int g;
        cin>>a;
        cin>>b;
        long long int x,y;
        x = pow(,a);
        y = pow(,b);
        g=gcd((x+y),(x*y));
        cout<<(x+y)/g<<'/'<<x*y/g<<endl;
    }
    return ;
}