Fourth session Blue Bridge Cup Javac Group _ Rational Number class

Source: Internet
Author: User
Tags gcd mul

/* (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

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.