/* (Start of program header comment)
* Copyright and version Declaration section of the program
* Copyright (c) 2016, Guangzhou Science and Technology Trade Vocational College, Department of Information Engineering students
* All rights reserved.
* File name: Blue Bridge Cup title
* Author: Pengjunhau
* Completion Date: April 01, 2016
* Version number: 001
* Description part of task and solution method
* Description of the problem:
A rational number is a number that can be represented as a ratio of two integers. In general, we use approximate decimal notation. But some
Time, the error is not allowed and a rational number must be represented by two integers.
At this point, we can create a "rational number class", the following code initially achieved this goal. To be concise, it only mentioned
For addition and multiplication operations.
Class Rational
{
Private long RA;
Private long RB;
Private long gcd (long A, long B) {
if (b==0) return A;
return gcd (B,A%B);
}
Public Rational (Long A, long B) {
RA = A;
RB = b;
Long k = gcd (RA,RB);
if (k>1) {//requires numerator
RA/= k;
RB/= K;
}
}
Addition
Public rational Add (rational x) {
return ________________________________________; Fill in the blanks
}
Multiplication
Public Rational Mul (rational x) {
return new Rational (Ra*x.ra, RB*X.RB);
}
Public String toString () {
if (rb==1) return "" + ra;
Return RA + "/" + RB;
}
}
Examples of using this class:
Rational a = new rational (1,3);
Rational B = new Rational (1,6);
Rational C = A.add (b);
System.out.println (A + "+" + B + "=" + C);
* Input Description:
* Program output:
* End of comment on the program head
*/
On the code:
public class Main {
public static void Main (string[] args) {
Rational a = new rational (1,3);
Rational B = new Rational (1,6);
Rational C = A.add (b);
System.out.println (A + "+" + B + "=" + C);
}
}
Class Rational
{
Private long RA;
Private long RB;
Private long gcd (long A, long B) {
if (b==0) return A;
return gcd (B,A%B);
}
Public Rational (Long A, long B) {
RA = A;
RB = b;
Long k = gcd (RA,RB);
if (k>1) {//requires numerator
RA/= k;
RB/= K;
}
}
Addition
Public rational Add (rational x) {
return new Rational (Ra*x.rb+rb*x.ra, RB*X.RB); Fill in the blanks
}
Multiplication
Public Rational Mul (rational x) {
return new Rational (Ra*x.ra, RB*X.RB);
}
Public String toString () {
if (rb==1) return "" + ra;
Return RA + "/" + RB;
}
}
Fourth session Blue Bridge Cup Javac Group _ Rational Number class