[PAT解题报告] Rational Arithmetic

和1081很像,多了一些运算, 仍然是直接做,通分,别忘记最大公约数。还有输出上注意负数要加括号。

代码:
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

char s[111];

long long gcd(long long x,long long y) {
    return y?gcd(y, x % y):x;
}

void print(long long x,long long y) {

    if (y < 0) {
        x = -x;
        y = -y;
    }
    long long g = gcd((x < 0)?(-x):x, y);
    x /= g;
    y /= g;
    if (x == 0) {
        putchar('0');
        return;
    }
    bool sign = false;
    if (x < 0) {
        sign = true;
        printf("(-");
        x = -x;
    }
    bool have = false;
    if (x / y) {
        printf("%lld",x / y);
        have = true;
    }
    if (x % y) {
        if (have) {
            putchar(' ');
        }
        printf("%lld/%lld",x % y, y);
    }
    if (sign) {
        putchar(')');
    }
}    

int main() {
long long a , b, c, d;
    scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
    // a / b + c / d
    print(a, b);
    printf(" + ");
    print(c, d);
    printf(" = ");
    print(a * d + b * c, b * d);
    puts("");
    
    print(a, b);    
    printf(" - ");
    print(c, d);
    printf(" = ");
    print(a * d - b * c, b * d);
    puts("");

    print(a, b);
    printf(" * ");
    print(c, d);
    printf(" = ");
    print(a * c, b * d);
    puts("");

    print(a, b);
    printf(" / ");
    print(c, d);
    printf(" = ");
    if (c) {
        print(a * d, b * c);
    }
    else {
        printf("Inf");
    }
    puts("");
    return 0;
}
原题链接: http://www.patest.cn/contests/pat-a-practise/1088
全部评论
你好可以帮我看下哪里出错了吗??谢谢 #include<iostream> #include<cstdio> using namespace std; long long lcm(long long x,long long y); long long gcd(long long x,long long y); void print(long long x,long long y); int main(){ long a1,b1,a2,b2,g;    //g用来存储最大公约数  scanf("%ld/%ld",&a1,&b1); scanf("%ld/%ld",&a2,&b2); long long m1=a1,n1=b1,m2=a2,n2=b2; if(m1!=0){ g=gcd(m1,n1); m1=m1/g; n1=n1/g; } if(m2!=0){ g=gcd(m2,n2); m2=m2/g; n2=n2/g; } long long l=lcm(b1,b2);   //最小公倍数 --和/差的分母  long long add=(l/b1)*a1+(l/b2)*a2;// 和的分子  long long minus=(l/b1)*a1-(l/b2)*a2; //差的分子 long long l2=l; if(add!=0){ g=gcd(add,l); add=add/g; l=l/g; } print(m1,n1); printf(" + "); print(m2,n2); printf(" = "); print(add,l); printf("\n"); if(minus!=0){ g=gcd(minus,l2); minus=minus/g; l2=l2/g; } print(m1,n1); printf(" - "); print(m2,n2); printf(" = "); print(minus,l2); printf("\n"); print(m1,n1); printf(" * "); print(m2,n2); printf(" = "); if(a1!=0&&a2!=0){     long long muz=a1*a2;//积的分子 long long mum=b1*b2; g=gcd(muz,mum); muz=muz/g; mum= mum/g; print(muz,mum); printf("\n"); }else{   //积为0  //long long muz=0;//积的分子 printf("0\n"); } print(m1,n1); printf(" / "); print(m2,n2); printf(" = "); if(a2==0){  //商为Inf printf("Inf\n"); }else{ if(a1==0)  { //dez=0;//商为0 printf("0\n"); } else{    long long dez=a1*b2;//商的分子 long long dem=b1*a2; g=gcd(dez,dem); dez=dez/g; dem= dem/g; print(dez,dem); printf("\n"); }  } return 0; } long long lcm(long long x,long long y){ long long i,l1,l2; long long top=l1*l2; for(i=1;i<=top;i++){ l1=x*i; l2=y*i; if(l1%y==0||l2%x==0) break; } if(l1%y==0) return l1; else return l2; } long long gcd(long long x,long long y){ long long g=1; x=x>0?x:-x; y=y>0?y:-y; long long min=x>y?y:x; long long i; for(i=2;i<=min;i++){ while(x%i==0&&y%i==0){ g=g*i; x=x/i; y=y/i; } min=x>y?y:x; }  return g; } void print(long long x,long long y){ if(x==0) { printf("0"); return; } if(y<0) { y=-y; x=-x; } if(x>0){ long long integer=x/y; long long mod=x%y; if(integer == 0) printf("%lld/%lld",x,y); else { if(mod==0) printf("%lld",integer); else printf("%lld %lld/%lld",integer,mod,y); } return; } if(x<0){ printf("("); x=-x; long long integer=-(x/y); long long mod=x%y; if(integer==0) printf("%lld/%lld",-x,y); else{ if(mod==0) printf("%lld",integer); else printf("%lld %lld/%lld",integer,mod,y); } printf(")"); return; } }
点赞 回复 分享
发布于 2015-08-24 02:01
什么jb题目,题目描述都写不清楚。
点赞 回复 分享
发布于 2022-09-03 14:40 福建

相关推荐

勤奋努力的椰子这就开摆:美团骑手在美团工作没毛病
投递美团等公司10个岗位
点赞 评论 收藏
分享
专心打鱼:互联网搬运工,贴子都要偷
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务