poj 3979(水题)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int gcd(int a,int b){
    int t;
    if(a==0)return b;
    while(b){
        t = a%b;
        a = b;
        b = t;
    }
    return a;
}
int main(){
    int t,a,b,c,d,p,q;
    char data[10];
    while(scanf("%s",data)!=EOF){
        a = data[0]-'0';
        b = data[2]-'0';
        c = data[4]-'0';
        d = data[6]-'0';
        if(data[3]=='-')
            c = -c;
        t = gcd(b,d);
        t = b*d/t;
        p = t/b*a+t/d*c;
        if(p==0){
            printf("0\n");
            continue;
        }
        q = gcd(t,p);
        if(q<0)q = -q;
        t/=q;
        p/=q;
        if(t==1){
            printf("%d\n",p);
            continue;
        }
        printf("%d/%d\n",p,t);
    }
    return 0;
}

 

上一篇:一行代码求两个数的最大公约数


下一篇:数论