Home > Back-end >  C language roughly divided into the minimalist scores
C language roughly divided into the minimalist scores

Time:12-02

Such as:
Scores can be expressed as the form of molecular/denominator, write a program that asks the user to enter a score, then roughly divided into the minimalist fraction, the simplest fraction is refers to the molecules and the denominator can not have a reduction of a fraction of the components, such as 6/12 can be roughly divided into 1/2, when molecules is greater than the denominator, don't need to express as an integer and fraction form, namely 11/8 or 11/8; When molecular denominator is equal, still expressed as 1/1 grade form,

Input format:
A score given in the input in a row, middle molecules and the denominator to slash/space, such as: 12/34 said 12/34, molecules and the denominator is a positive integer (does not contain 0, if it is not clear the definition of a positive integer),

Tip: in the scanf/is added to the format string, let the scanf to deal with the slash,

The output format:
Output in one line the points for the simplest fraction, format the same as the input, namely in the form of molecular/denominator said scores, such as 5/6 said 5/6,

Input the sample:
66/120
The output sample:
11/20



# include "stdio.h"
Int main ()
{
Int a, b, c;
Char x;
The scanf (" % d % d % c ", & amp; A, & amp; X, & amp; C);
For (b=2; B++)
{
If (a==1 | | c==1)
break;
If (a % b==0 & amp; & C % b==0)
{
A=a/b;
C=c/b;
}

}

Printf (" % d/% d \ n ", a, c);

}
I handed in by the code in the pta, prompt time, have a great god can you help me to improve my speed up the program is running,

CodePudding user response:

# include "stdio.h"
Int main ()
{
Int a, b, c, Max, min,
Char x;
The scanf (" % d % d % c ", & amp; A, & amp; X, & amp; C);
If (a> C)
{
Max=a;
Min=c; }
The else {
Max=c;
Min=a;
}
For (b=min; b>=1; B++)
{
If (a==1 | | c==1)
break;
If (a % b==0 & amp; & C % b==0)
{
A=a/b;
C=c/b;
}

}

Printf (" % d/% d \ n ", a, c);

}, to supplement and improve the code, but still running overtime,,, to speed up
  • Related