Structure-05. Mean value of rational number (20) time limit MS Memory limit 65536 KB code length limit 8000 B award Program StandardAuthor Joleen (Tsinghua University)
A program is required to calculate the average of n rational numbers.
Input format:
Enter the 1th line to give the positive integer n (<=100), and the 2nd line in the format "a1/b1 a2/b2 ..." gives a rational number in the form of n fractions, where the numerator and denominator are all integers in the shaping range, and if negative, the negative sign must appear at the front.
Output format:
Outputs the average of n rational numbers in a row in a "A/b" format. Note must be the simplest fractional form of the rational number, and if the denominator is 1, only the molecules are output.
Input Sample 1:
41/2 1/6 3/6 -5/10
Output Example 1:
1/6
Input Sample 2:
24/3 2/3
Output Example 2:
1
1#include <stdio.h>2#include <math.h>3#include <stdlib.h>4#include <string.h>5 intMain ()6 {7 structRational8 {9 intA;Ten intb; One }; ARational X, r[ the]; - inti, N; -scanf"%d", &n); the for(i =0; I < n; i++) - { -scanf"%d/%d", &R[I].A, &r[i].b); - } +X.A =0; -x.b =1; + for(i =0; I < n; i++) A { atX.A = x.a * r[i].b + x.b *r[i].a; -X.B *=r[i].b; - } -X.B *=N; - inttemp, H, l; -h = x.a > x.b?x.a:x.b; inL = x.a < x.b?x.a:x.b; - while(L) to { +temp = h%l; -h =l; theL =temp; * } $ if(H! =0)Panax Notoginseng { -X.A/=h; theX.B/=h; + } A if(X.A = =0) theprintf"%d\n", x.a); + Else if(x.b = =1) -printf"%d\n", x.a); $ Else $printf"%d/%d\n", X.A, x.b); - return 0; - return 0; the}
Structure-05. Mean value of rational number