[Daily Blue Bridge] 5, 13 years of provincial competition Java group real questions "Rational Numbers"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Rational Numbers

A rational number is a number that can be expressed as the ratio of two integers. Under normal circumstances, we use approximate decimals to express, but sometimes errors are not allowed and must be expressed by two integers. At this time, we can create a "rational number class" , The following code initially achieves this goal. For simplicity, it only provides addition and multiplication operations.

static class Rotional{
		private long ra;
		private long rb;
		
		public Rotional(long a,long b) {
			ra = a;
			rb = b;
			long k = gcd(ra, rb);	//获得最大公约数
			if (k>1) {	//如果k大于1,则说明需要约分
				ra /= k;
				rb /= k;
			}
		}
		
		/**
		 * 求最大公约数
		 * */
		private long gcd(long a,long b) {
			if (b==0) {
				return a;
			}
			return gcd(b, a%b);
		}
		
		/**
		 * 加法
		 * */
		public Rotional add(Rotional x) {
			return ________________________________;	//填空位置
		}
		
		/**
		 * 乘法
		 * */
		public Rotional mul(Rotional x) {
			return new Rotional(ra*x.ra, rb*x.rb);
		}
		
		@Override
		public String toString() {
			if (rb==1) {
				return "" + ra;
			}
			return ra + "/" + rb;
		}
	}

Examples of using this class:

Rotional a = new Rotional(1, 3);

Rotional b = new Rotional(1, 6);

Rotional c = a.add(b);

System.out.println(a + "+" + b + "=" + c);

 

Please analyze the code logic, and guess the code at the underline, and submit it through the web

Note: Only use the missing code as the answer, do not fill in extra codes, symbols or explanatory text! ! !

Problem-solving ideas:

The key to solving this problem is to read through the source code in the stem, understand the specific functions to be implemented in each line, and then understand the main idea of ​​the program design, and fill in the blanks according to the requirements in the stem.

The main content examined in this question is actually the object-oriented thinking in Java. The object-oriented design method is used to realize the addition and multiplication between two fractions, and the calculation process of the multiplication has been given in the title. Can refer to. So through the understanding of the code, we can get the corresponding operation process of addition.

Complete source code:

package 一三年省赛真题;

public class Year2013_t5 {

	public static void main(String[] args) {
		Rotional a = new Rotional(1, 3);
		Rotional b = new Rotional(1, 6);
		Rotional c = a.add(b);
		System.out.println(a + "+" + b + "=" + c);
	}
	
	static class Rotional{
		private long ra;
		private long rb;
		
		public Rotional(long a,long b) {
			ra = a;
			rb = b;
			long k = gcd(ra, rb);	//获得最大公约数
			if (k>1) {	//如果k大于1,则说明需要约分
				ra /= k;
				rb /= k;
			}
		}
		
		/**
		 * 求最大公约数
		 * */
		private long gcd(long a,long b) {
			if (b==0) {
				return a;
			}
			return gcd(b, a%b);
		}
		
		/**
		 * 加法
		 * */
		public Rotional add(Rotional x) {
			return new Rotional(this.ra*x.rb + x.ra*this.rb, this.rb*x.rb);	//填空位置
		}
		
		/**
		 * 乘法
		 * */
		public Rotional mul(Rotional x) {
			return new Rotional(ra*x.ra, rb*x.rb);
		}
		
		@Override
		public String toString() {
			if (rb==1) {
				return "" + ra;
			}
			return ra + "/" + rb;
		}
	}

}

Sample output:

There are deficiencies or improvements, and I hope my friends will leave a message and learn together!

Interested friends can follow the column!

Little Grey Ape will accompany you to make progress together!

Finally, I am participating in the selection of the 2020 Blog Star, please help me to vote for it!

Vote link: https://bss.csdn.net/m/topic/blog_star2020/detail?username=weixin_44985880

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/112602785