05-1. Minimum fraction (15)

Source: Internet
Author: User

Scores can be expressed as "Numerator/Denominator. Write a program that requires the user to enter a score and divide the score into the simplest fraction. The simplest fraction means that the numerator and denominator do not have any components that can be divided. For example, 6/12 can be divided into about 1/2. When the numerator is greater than the denominator, it does not need to be expressed as an integer or a score, that is, 11/8 or 11/8. When the denominator is equal, it is still expressed as a score of 1/1.

Input Format:

Enter a score in a row. The numerator and denominator are separated by a slash (/), for example:12/34It indicates 12 out of 34. Both the numerator and denominator are positive integers (excluding 0, if the definition of positive integers is unclear ).

Tip: Add "/" to the scanf format string to let scanf handle this slash.

Output Format:

Output the simplest fraction corresponding to the score in a row. The format is the same as that of the Input. That is, the score is expressed in the form of "Numerator/Denominator. For example5/6It indicates 5 out of 6.

Input example:

60/120

Output example:

1/2

 

Note: This question requires an appointment. In fact, it is to calculate the maximum denominator of a numerator, and then divide the denominator by the maximum denominator. The moving phase division is used to calculate the maximum common divisor.

#include "stdio.h"int main(){    int fz,fm,t;    scanf("%d/%d",&fz,&fm);    int origa=fz;    int origb=fm;    while(fm!=0)    {        t=fz%fm;        fz=fm;        fm=t;    }    printf("%d/%d",origa/fz,origb/fz);    return 0;}

05-1. Minimum fraction (15)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.