Problem Description
This day the teacher gave small Hao a very simple topic relaxed: Enter a score, let you find their simplest score.
Input
The first line includes a T, which represents the number of groups of test data.
The next line of T lines includes a fraction. (The numerator denominator is within the int range)
Output
For each test sample, the output line has its simplest fraction.
Sample Input
32/63/111/36
Sample Output
1/33/111/36
My Code:
1#include <stdio.h>2ProcessintAintb/*greatest common divisor, so that the numerator function*/3 {4 intC;5 while(b!=0)6 {7c=a%b;8A=b;9b=C;Ten } One returnA; A } - - voidMain () the { - intn,i,a,b,min; -scanf"%d",&n); - for(i=0; i<n;i++) + { -scanf"%d/%d",&a,&b); +min=process (A, b); Aprintf"%d/%d\n", a/min,b/min); at } -}
Other code:
1#include <iostream>2 3 using namespacestd;4 intMGCD (intAintb)5 {6 7 intT;8 if(a<b)9 {Ten Onet=a;a=b;b=T; A } - while(a%b) - { thet=b; -b=a%b; -A=T; - } + returnb; - } + A intMain () at { - inta,b,t; - Charch; - intCount=0; - intT; -Cin>>T; in while(cin>>a>>ch>>b) - { to++count; +t=MGCD (A, b); -A/=T; theb/=T; *cout<<a<<"/"<<b<<Endl; $ if(count==T)Panax Notoginseng { - Break; the } + } A the return 0; +}
Wuhan University of Science and Technology acm:1007: Chinese Edition C language Programming tutorial (second edition) exercise 5.7