[Daily Blue Bridge] 7. The real question of "the number of walnuts" in the Java group in the first and three years of provincial competitions

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: Number of Walnuts

Xiao Zhang is a software project manager. He leads three development teams with tight schedules. They are all working overtime today. To boost morale, Xiao Zhang intends to send a bag of walnuts to each team (according to rumors, it can help the brain). His request Yes:

  1. The number of walnuts in each group must be the same

  2. The walnuts must be equally divided in each group (of course they cannot be broken)

  3. Try to provide the smallest quantity that satisfies the conditions of 1, 2 (saving and making revolution)

The program reads in from standard input:

a b c

a, b, c are all positive integers, indicating the number of people working overtime in each group, separated by spaces (a, b, c<30)

Program output:

A positive integer, indicating the number of walnuts per bag

E.g:

User input:

2 4 5

Program output:

20

 

Another example:

User input:

3 1 1

Program input:

3

 

Resource agreement:

Peak memory consumption (including virtual machines) <64M

CPU consumption <1000ms

Please input strictly according to the requirements, and do not superfluously print redundant content like: "Please input..."

All codes are placed in the same source file, after the test passes, copy and submit the source code

Note: the main function needs to return 0

Note: Only use ANSI C/ANSI C++ standards, do not call special functions that depend on the compilation environment or operating system

Note: All dependent functions must be clearly in the source file, #include<xxx> can not be set through the project to ignore common header files

When submitting, pay attention to selecting the desired compiler type

 

Problem-solving ideas:

According to the requirements in the question stem, we can infer that this is actually a question of finding the least common multiple of three numbers, so we can solve the problem according to the method of solving the least common multiple.

Here I have listed two solving methods, one is the simpler method to directly solve the least common multiple of three numbers, and the second is to use the least common multiple of two numbers to solve the problem.

At the same time, here is a summary of the method of solving the least common multiple of two numbers:

Least common multiple of two numbers = product of two numbers / greatest common divisor of two numbers

Finding the greatest common divisor of two numbers can be solved by the division method:
here is a list of Java solutions:

/**
* Find the least common multiple of two numbers
 *
@param  xy parameters passed in
 * */

public  static  int  minMultiple( int  a , int  b ) {
     return  a * b / maxApp ( a , b );
}

/**
 * Find the greatest common divisor of two numbers
 *
@param  x, y The parameters passed in
 * */

public  static  int  maxApp( int  a , int  b ) {
     if  ( a < b) {
          int temp = a;
          a = b;
          b = temp;
          }

     int r = a%b;
     while (r!=0) {
          a = b;
          b = r;
          r = a%b;
   }

          return b;
}

 

Answer source code :

Solution one (direct solution):

public class Year2013_t7 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		int c = scanner.nextInt();
		
		for (int i = 1; i <= a*b*c; i++) {
			if (i%a==0&&i%b==0&&i%c==0) {
				System.out.println(i);
				break;
			}
		}
	}
}

Solution two (seeking the least common multiple):

package 一三年省赛真题;

import java.util.Scanner;

public class Year2013_t7 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		int c = scanner.nextInt();
		System.out.println(minMultiple(a, minMultiple(b, c)));
		
	}
	
	/**
	 * 求两个数的最小公倍数
	 * @param x y 传入的参数
	 * */
	public static int minMultiple(int a,int b) {
		return a*b/maxApp(a, b);
	}
	
	/**
	 * 求两个数的最大公约数
	 * @param x, y 传入的参数
	 * */
	public static int maxApp(int a, int b) {
		if (a<b) {
			int temp = a;
			a = b;
			b = temp;
		}
		int r = a%b;
		while (r!=0) {
			a = b;
			b = r;
			r = a%b;
		}
		return b;
	}
}

 

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/112694474