337B - Routine Problem - CodeForces Solution


greedy math number theory *1400

Please click on ads to support us..

Python Code:

def gcd(a, b):
    while a % b != 0:
        a, b = b, a % b
    return b

l = input().split(' ')
a, b, c, d = int(l[0]), int(l[1]), int(l[2]), int(l[3])
if a * d > b * c:
    p = a * d - b * c
    q = a * d
else:
    p = b * c - a * d
    q = b * c
e = gcd(p, q)
p //= e
q //= e
print(str(p) + "/" + str(q))

C++ Code:

#include <bits/stdc++.h>


using namespace std;

int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
  // int minab=min(a,b);
 //  int maxab=max(a,b);
  // int mincd=min(c,d);
   //int maxcd=max(c,d);
 // double ans=1-(((mincd/maxcd)*minab)/maxab);
    //int x = __gcd(maxcd*maxab-minab*mincd,maxcd*maxab);
    //printf("%d/%d",(maxcd*maxab-minab*mincd)/x,(maxcd*maxab)/x);
  if(a*d>b*c){
      swap(a,b);swap(c,d);
  }
  int p=(b*c-a*d) ;
  int q=b*c;
  int x=__gcd(p,q);
  cout<<p/x<<"/"<<q/x;
    
}
   			 		      	 	   		   			


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST