【北邮OJ】266. 分数加法-网研14

来源:互联网 发布:画中画软件下载 编辑:程序博客网 时间:2024/04/30 09:39

总结:
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 == 0) {return a;}    else {return gcd(b,a%b);}}int main(){    int t,T;    cin>>T;    for(t=1;t<=T;t++){        long long int a,b;        long long int g;        cin>>a;        cin>>b;        long long int x,y;        x = pow(2,a);        y = pow(2,b);        g=gcd((x+y),(x*y));        cout<<(x+y)/g<<'/'<<x*y/g<<endl;    }    return 0;}
0 0
原创粉丝点击