CCC '02 S2 - Fraction Action

View as PDF

Submit solution

Points: 5
Time limit: 0.5s
Memory limit: 256M

Problem type
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


  • 0
    _ryan6_  commented on Jan. 10, 2023, 9:39 p.m.

    What's the format? I tried both a new line at the end and no new line at the end, neither worked :(


    • 0
      _ryan6_  commented on Jan. 13, 2023, 12:51 a.m.

      o i got it now, because i forgot to write kinds with only a number :(


    • 1
      BalintR  commented on Jan. 11, 2023, 12:25 a.m.

      Your code outputs an unnecessary space character when the result is a non-zero integer. For example, for the first sample, the output of your program is equivalent to System.out.print("4 \n");.

      Unless explicitly stated otherwise, you should assume that problems have input/output with each line being terminated by a \n character with no trailing whitespace.


  • 2
    finity4037  commented on Dec. 20, 2021, 8:45 p.m.

    I'm always getting the third test case wrong but my code looks fine by me. I ran it by multiple g5 improper to mixed fractions worksheets and they've all worked; would appreciate anyone who can help.


    • 6
      ahassini04  commented on Dec. 22, 2021, 9:38 p.m.

      I had the same problem, run your code with this input 12 42. Your output will most probably be 0 2/7. You should not be printing 0's in these cases.


      • 2
        finity4037  commented on Jan. 23, 2022, 5:19 p.m.

        Ah that was the problem, thank you


  • 9
    EdZ123  commented on March 27, 2021, 4:17 p.m.

    For those getting presentation error make sure to print a newline at the end


  • 1
    AndrewC  commented on July 12, 2020, 2:19 a.m.

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


    • 5
      jeffreykaili  commented on July 12, 2020, 2:41 a.m.

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


      • 2
        AndrewC  commented on July 12, 2020, 3:28 a.m.

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


  • 0
    FredeEmnetu  commented on Dec. 6, 2017, 3:53 a.m.

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


    • 9
      jkguipqnjcy49979693  commented on Dec. 6, 2017, 4:35 a.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 12, 2015, 12:42 a.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


    • 31
      kobortor  commented on March 12, 2015, 12:59 a.m.

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