The third question of Java Blue Bridge Cup 2014

Number of Java Walnuts

Xiao Zhang is a software project manager who leads 3 development teams. The schedule is tight, I am working overtime today. To boost morale, Zhang intends to give
each group a bag of walnuts (rumored to be able to nourish the brain). His request is:

  1. The number of walnuts in each group must be the same
  2. Each group must be able to divide the walnuts equally (of course they cannot be broken)
  3. Try to provide the minimum number that satisfies the conditions of 1 and 2 (thrift and revolution)
  • Enter description:

The program reads from the standard input:
abc
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:

Program output:
A positive integer representing the number of walnuts per bag.

For example:
User input:
2 4 5

Program output:
20

Another example:
user input:
3 1 1

Program output:
3

public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
 
		int sum = a * b * c;
		for (int i = 1; i < sum; i++) {
    
    
			if (i % a == 0 && i % b == 0 && i % c == 0) {
    
    
				sum = i;
				break;
			}
		}
		System.out.println(sum);
	}

This problem is to find the least common multiple. We initially locate the least common multiple to multiply three numbers, because the multiplication of three numbers must be the common multiple of these three numbers, and then find the smallest common multiple among the numbers multiplied by three numbers. If found, Set the least common multiple to i, otherwise the multiplication of three numbers is the least common multiple!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325173138&siteId=291194637
Recommended