Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

心情简单地水一水~

Posted by 947186602 at 2020-09-01 21:06:45 on Problem 3979
#include<cstdio>
#include<string.h>
using namespace std;
char s[10];

int gcd(int a,int b){
     return b == 0 ? a : gcd(b,a%b);
}

int main(){
     while(~scanf("%s",s)){
          int x1 = s[0]-'0',y1 = s[2]-'0',x2 = s[4]-'0',y2 = s[6]-'0';
          char way = s[3];
          int all = y1 * y2 / gcd(y1,y2);
          //printf("%d\n",all);
          int now_x;
          if(way == '-'){
               now_x = x1 * all / y1 - x2 * all / y2;
          }else{
               now_x = x1 * all / y1 + x2 * all / y2;
          }
          if(now_x % all == 0){  // 0 1 2 ...
               printf("%d\n",now_x / all);
          }
          else {
               if(now_x < 0) {
                    printf("-");
                    now_x = -now_x;
               }
               int t = gcd(now_x,all);
               printf("%d/%d\n",now_x / t,all / t);
          }
     }
     return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator