Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.33 KB | None | 0 0
  1. class Fraction(private val numenator: Long, private val denominator: Long): Comparable<Fraction> {
  2.     override fun compareTo(other: Fraction): Int {
  3.         val diff = numenator * other.denominator - other.numenator * denominator
  4.         return if (diff < 0) -1 else if (diff == 0L) 0 else 1
  5.     }
  6.  
  7.     override fun toString(): String {
  8.         return "$numenator/$denominator"
  9.     }
  10. }
  11.  
  12. fun readLongs() = readLine()!!.split(' ').map { it.toLong() }
  13.  
  14. fun gcd(a: Long, b: Long): Long {
  15.     if (b == 0L) {
  16.         return a
  17.     }
  18.     return gcd(b, a % b)
  19. }
  20.  
  21. fun lcm(a: Long, b: Long) = (a / gcd(a, b)) * b
  22.  
  23. fun main() {
  24.     val (a, b, c, d) = readLongs()
  25.     val gcdAB = gcd(a, b)
  26.     val gcdAC = gcd(a, c)
  27.     val gcdAD = gcd(a, d)
  28.     val gcdBC = gcd(b, c)
  29.     val gcdBD = gcd(b, d)
  30.     val gcdCD = gcd(c, d)
  31.  
  32.     val fractions = mutableListOf<Fraction>()
  33.     fractions.add(Fraction(a, b))
  34.     fractions.add(Fraction(a, c))
  35.     fractions.add(Fraction(a, d))
  36.  
  37.     fractions.add(Fraction(b, a))
  38.     fractions.add(Fraction(b, c))
  39.     fractions.add(Fraction(b, d))
  40.  
  41.     fractions.add(Fraction(c, a))
  42.     fractions.add(Fraction(c, b))
  43.     fractions.add(Fraction(c, d))
  44.  
  45.     fractions.add(Fraction(d, a))
  46.     fractions.add(Fraction(d, b))
  47.     fractions.add(Fraction(d, c))
  48.  
  49.     fractions.sort()
  50.     println(fractions[1])
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement