* Structure-05. rational number mean, structure-05 rational number mean

Source: Internet
Author: User

* Structure-05. rational number mean, structure-05 rational number mean

1/* 2 * Main. c 3 * F5-structure-05. rational Number Average 4 * Created on: August 26, 2014 5 * Author: in the Boomkeeper 6 ********** part, the ** floating point error ******* 7 */8 9 # include <stdio. h> 10/** 11 * rational number struct 12 */13 struct rational {14 int numerator; 15 int denominator; 16 }; 17/** 18 * maximum common approx. 19 */20 int greatCommonDivisor (int m, int n) {21 int I; 22 for (I = (m <n? M: n); I> 1; I --) {23 if (m % I = 0 & n % I = 0) 24 break; 25} 26 return I; 27} 28 29 int main (void) {30 31 int n; // N32 struct rational num [101]; 33 struct rational result; 34 int I; 35 36 scanf ("% d", & n); 37 getchar (); 38 39 for (I = 0; I <n; I ++) {40 scanf ("% d/% d", & num [I]. numerator, & num [I]. denominator); 41} 42 43 result. numerator = num [0]. numerator; 44 result. denominator = num [0]. denominator; 45 46 // sum of points 47 for (I = 1; I <n; I ++) {48 result. numerator = result. numerator * num [I]. denominator49 + num [I]. numerator * result. denominator; 50 result. denominator = result. denominator * num [I]. denominator; 51 if (result. numerator! = 0) {52 // avoid accumulating and exceeding the int range. After each accumulation, divide about 53 int gcd = greatCommonDivisor (result. numerator, result. denominator); 54 result. numerator/= gcd; 55 result. denominator/= gcd; 56} 57 58} 59 60 // calculate the average 61 result. denominator * = n; 62 int gcd = greatCommonDivisor (result. numerator, result. denominator); 63 result. numerator/= gcd; 64 result. denominator/= gcd; 65 66 // output result 67 if (result. denominator = 1) 68 printf ("% d \ n", result. numerator); 69 else70 printf ("% d/% d \ n", result. numerator, result. denominator); 71 72 return 0; 73}

 

Question link:

Http://pat.zju.edu.cn/contests/basic-programming/%E7%BB%93%E6%9E%84-05

 


First, input the scores of N students in the M course on the keyboard, and then calculate and output the total scores and average values of each student.

It's not hard.
# Include <stdio. h>
# Define N 3
# Define M 3
Main ()
{
Int a [N] [M], sum [N] = {0 };
Double avg [N];
Int I, j;
For (I = 0; I <N; I ++)
{
Printf ("Input the % dth student's scores:", I + 1 );
For (j = 0; j <M; j ++)
{
Scanf ("% d", & a [I] [j]);
Sum [I] + = a [I] [j];
}
Avg [I] = (double) sum [I]/M;
}
Printf ("The sums are :");
For (I = 0; I <N; I ++)
Printf ("% 4d", sum [I]);
Printf ("\ nThe averages are :");
For (I = 0; I <N; I ++)
Printf ("% 6.2lf", avg [I]);
Printf ("\ n ");
}
// OK I wish it will be help.

What is the difference between 7 days and 4 months of pregnancy?

I don't think you need to be nervous. I used to have such a situation during B-ultrasound! At that time, I was very anxious to ask the doctor. In fact, it was because the location of the child was constantly changing. The B-ultrasound doctor was not very clear! For example, if the child is lying down, the doctor will write a facial presentation that is not satisfied! Haha! You can rest assured!

Related Article

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.