CCC '02 S2 - Fraction Action

View as PDF

Submit solution

Points: 5
Time limit: 2.0s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
Canadian Computing Competition: 2002 Stage 1, Junior #4, Senior #2

Many advanced calculators have a fraction feature that will simplify fractions for you. You are to write a program that will accept for input a non-negative integer as a numerator and a positive integer as a denominator, and output the fraction in simplest form. That is, the fraction cannot be reduced any further, and the numerator will be less than the denominator. You can assume that all input numerators and denominators will produce valid fractions.

Sample Input 1

28
7

Sample Output 1

4

Sample Input 2

13
5

Sample Output 2

2 3/5

Sample Input 3

0
7

Sample Output 3

0

Sample Input 4

55
10

Sample Output 4

5 1/2

Comments


  • 2
    AndreS  commented on Sept. 11, 2020, 11:02 p.m.

    Good Question. I approve.


  • 1
    AndrewC  commented on July 11, 2020, 10:19 p.m.

    Does anyone know why my code is not working? Help would be much appreciated!


    • 2
      jeffreykaili  commented on July 11, 2020, 10:41 p.m.

      On sample 4, your most recent submission outputs "5 5/10" instead of "5 1/2"


      • 2
        AndrewC  commented on July 11, 2020, 11:28 p.m.

        Thank you so much! I have finally solved the problem :)


  • 0
    Julien  commented on Jan. 11, 2020, 10:14 p.m.

    Can someone please explain why problem 5 is not working for me?


  • -6
    Xenosi  commented on Nov. 28, 2019, 3:35 p.m. edit 2

    This comment is hidden due to too much negative feedback. Click here to view it.


    • 2
      owenc  commented on July 10, 2020, 11:21 a.m.

      the denominator can't be zero


  • 0
    YounessTEA  commented on Dec. 22, 2017, 5:15 p.m.

    For test 4 and 5 it keeps getting wrong where is the problem please help.


    • -32
      reda2002  commented on Dec. 23, 2017, 12:48 p.m.

      This comment is hidden due to too much negative feedback. Click here to view it.


  • -1
    FredeEmnetu  commented on Dec. 5, 2017, 10:53 p.m.

    For test case 4 it's 4 0/3 which is just 4 but I keep getting it wrong. Help


    • 6
      jkguipqnjcy49979693  commented on Dec. 5, 2017, 11:35 p.m.

      Your code suffers from precision issues (that is, a value of 1 in double ends up being "stored" as 0.9999999.... and that gets rounded down)

      As a good rule of thumb, whenever you can avoid floating point arithmetic, you should.


  • -1
    minecraftyugi  commented on March 11, 2015, 8:42 p.m.

    If the answer turns out to be 0 and a fraction, do we just need to print 0? I keep getting a wrong answer on the answers with 0 in them


    • 22
      kobortor  commented on March 11, 2015, 8:59 p.m.

      If the answer is 0 1/2, don't print the 0