From about 7-24 minutes simplest fractions (15point (s)). C

It can be expressed as fractional molecular / denominator form. Write a program that requires users to enter a fraction, which is then divided into about the most simple fraction. The simplest fractional numerator and the denominator refers to a component that does not have to be about a minute. As can be divided into 6/12 to about 1/2. When the molecule is greater than the denominator, and need not expressed as a fraction of an integer of the form, i.e., 11/8 or 11/8; when the numerator and denominator are equal, still expressed as a fraction in the form of 1/1.
Input formats:

Input is given a score in a row, the numerator and denominator intermediate slash / separator, such as: 12/34 12/34 represents. Numerator and denominator are positive integers (not including 0, if it is unclear if the definition of a positive integer).

Tip: Add in the format string in scanf /, let scanf to deal with the slash.
Output formats:

In the same row output simplest fraction, the fraction corresponding to the input format, i.e., in the form of molecular / denominator represent fractions. The 5/6 indicates 5/6.
Sample input:

66/120

Sample output:

11/20
Idea : Select structure ; the core algorithm : Algorithm of division

//  date:2020/3/3
//  author:xiezhg5
#include <stdio.h>
int main(void)
{
    int a,b,r,c,temp,p,q;       //temp交换a,b用的中间量 
    char d='/';
    scanf("%d%c%d",&a,&d,&b);
    p=a;                      //p储存a 
    q=b;                      //q储存b 
    if(b<a)
    {
    	//交换a和b的值,确保b大于a(做除数) 
        temp=b;
        b=a;
        a=temp;
    }
    while(a!=0)           //化简怎么能少最大公约数呢 
    {
        r=b%a;
        b=a;
        a=r;
    }
    //两种情况(不需化简和需要化简) 
    if(p%b==0&&q%b==0)    
    printf("%d/%d",p/b,q/b);
    else
    printf("%d/%d",p,q);
    return 0;
}
Published 30 original articles · won praise 10 · views 165

Guess you like

Origin blog.csdn.net/qq_45645641/article/details/104705375