Structure-05. Mean value of rational number (20)

Source: Internet
Author: User
Tags greatest common divisor

Source of the topic:

Structure-05. Mean value of rational number (20) time limit MS
Memory Limit 65536 KB
Code length limit 8000 B
Procedures for the award of questions 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

Solution 1:

Import Java.text.decimalformat;import java.util.arraylist;import Java.util.arrays;import Java.util.HashMap;import Java.util.iterator;import Java.util.linkedhashset;import Java.util.linkedlist;import Java.util.Scanner;public  Class Main {public static void Main (string[] args) {Scanner Scanner = new Scanner (system.in);  positive integer n int n = scanner.nextint ();  Numerator, denominator int fenzi=0;  int fenmu=1;  for (int i=0;i<n;i++) {//Enter a fraction, a, b to accept numerator and denominator string[] str = Scanner.next (). Split ("/");  int a = Integer.parseint (str[0]);  int b = Integer.parseint (str[1]);  Two fractions added fenzi = FENZI*B+A*FENMU;  Fenmu = fenmu*b;  }//After the denominator *n, once again simplify FENMU = fenmu*n;  The most simplified of the numerator and denominator t int t = GCD (FENZI,FENMU);  if (t!=0) {fenzi = fenzi/t;  Fenmu = fenmu/t;  } if (fenzi==0) {System.out.println (Fenzi);  } else if (fenmu==1) System.out.println (Fenzi);  else {System.out.println (fenzi+ "/" +FENMU); }}/* * This is a greedy algorithm. Set greatest common divisor to X, the existence of an integer i,j makes: a = I*x,b = J*x and because C = a% B so there is an integer k makes: c = a-k*b = I*x-k*j*x = (i-j*k) *X is the number of conventions where x is also C, then A = B; b = c; So loop, there is always B = k*a, then B is greatest common divisor.      * */public static int gcd (int fenzi,int fenmu) {int r;      if (Fenmu = = 0 && Fenzi = = 0) return 0;      if (Fenmu = = 0) return fenzi;      if (Fenzi = = 0) return FENMU;          while (true) {r = fenmu% Fenzi;          if (r = = 0) break;          Fenmu = Fenzi;      Fenzi = R;   } return Fenzi;           }}


Solution 2:

Import Java.text.decimalformat;import java.util.arraylist;import Java.util.arrays;import Java.util.HashMap;import Java.util.iterator;import Java.util.linkedhashset;import Java.util.linkedlist;import Java.util.Scanner;public  Class Main {public static void Main (string[] args) {Scanner Scanner = new Scanner (system.in);  positive integer n int n = scanner.nextint ();  Numerator, denominator int fenzi=0;  int fenmu=1;  for (int i=0;i<n;i++) {//Enter a fraction, a, b to accept numerator and denominator string[] str = Scanner.next (). Split ("/");  int a = Integer.parseint (str[0]);  int b = Integer.parseint (str[1]);  Two fractions added fenzi = FENZI*B+A*FENMU;  Fenmu = fenmu*b;  }//After the denominator *n, once again simplify FENMU = fenmu*n;  The simplest of the numerator and denominator t int t = simple (FENZI,FENMU);  if (t!=0) {fenzi = fenzi/t;  Fenmu = fenmu/t;  } if (fenzi==0) {System.out.println (Fenzi);  return;  }//Whether the token is a negative Boolean flag =false;  if (fenzi*fenmu<0)//test 1/-2-1/2 {flag = true;  System.out.print ("-");  }//Output minus if (fenmu==1) System.out.println (Math.Abs (Fenzi)); else {SyStem.out.println (Math.Abs (Fenzi) + "/" +math.abs (FENMU)); }} public static int simple (int fenzi,int fenmu) {int min = math.min (Math.Abs (Fenzi), Math.Abs (FENMU)), and for (int i=min;i >1;--i) {if (fenzi%i==0 && fenmu%i==0) {return i;}} return 0;           } }






Structure-05. Mean value of rational number (20)

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.