a x b = c
将a,b,c用out统一输出
AC代码:
#include <cstdio> #include <string> using namespace std; long long gcd(long long x,long long y){ if(x == 0) return y; if(y == 0) return x; if(x < 0) x = -x; if(y < 0) y = -y; long long max,min; max = x > y ? x : y; min = x > y ? y : x; while(max % min != 0){ long long tmp(min); min = max % min; max = tmp; } return min; } void out(long long an,long long ad){ if(ad == 0){ printf("Inf"); return; } if(ad < 0){ ad = -ad; an = -an; } long long ai,g; ai = an / ad; an = an - ai * ad; g = gcd(an,ad); an /= g; ad /= g; if(ai != 0){ an = an > -an ? an : -an; if(an != 0){ if(ai < 0){ printf("(%lld %lld/%lld)",ai,an,ad); } else{ printf("%lld %lld/%lld",ai,an,ad); } } else{ if(ai < 0){ printf("(%lld)",ai); } else printf("%lld",ai); } } else{ if(an == 0) printf("0"); else if(an < 0) printf("(%lld/%lld)",an,ad); else printf("%lld/%lld",an,ad); } } int main(){ long long an,ad,bn,bd; scanf("%lld/%lld %lld/%lld",&an,&ad,&bn,&bd); out(an,ad); printf(" + "); out(bn,bd); printf(" = "); out(an*bd+bn*ad,ad*bd); printf(" "); out(an,ad); printf(" - "); out(bn,bd); printf(" = "); out(an*bd-bn*ad,ad*bd); printf(" "); out(an,ad); printf(" * "); out(bn,bd); printf(" = "); out(an*bn,ad*bd); printf(" "); out(an,ad); printf(" / "); out(bn,bd); printf(" = "); out(an*bd,ad*bn); printf(" "); return 0; }