Blue Bridge Cup Test Questions Previous Test Questions Divided Chocolate (Java Solution)

Blue Bridge Cup Test Questions Previous Test Questions Divided Chocolate (Java Solution)

Problem Description

On Children's Day, K children visited Xiao Ming's house as guests. Xiao Ming took out a collection of chocolates to entertain the children.
Xiao Ming has N pieces of chocolate in total, of which the i-th piece is a rectangle composed of Hi x Wi squares.

To be fair, Xiao Ming needs to cut out K pieces of chocolate from the N pieces of chocolate to share with the children. The cut chocolate needs to meet:

  1. The shape is square and the side length is an integer
  2. Same size

For example, a piece of 6x5 chocolate can cut into 6 pieces of 2x2 chocolate or 2 pieces of 3x3 chocolate.

Of course, the kids all hope that the chocolate they get is as big as possible. Can you help Little Hi calculate the maximum side length?

Input format

The first line contains two integers N and K. (1 <= N, K <= 100000)
Each of the following N lines contains two integers Hi and Wi. (1 <= Hi, Wi <= 100000)
Enter to ensure that each child can get at least a 1x1 chocolate.

Output format

Output the maximum possible side length of the cut square chocolate.

Sample input

2 10
6 5
5 6

Sample output

2

Code

import java.util.Scanner;

public class Main {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		// n 块巧克力,k 个小朋友
		int n,k;
		n = sc.nextInt();
		k = sc.nextInt();
		int[] h = new int[n];
		int[] w = new int[n];
		int maxh,maxw;
		h[0] = sc.nextInt();
		w[0] = sc.nextInt();
		maxh = h[0];
		maxw = w[0];
		for (int i = 1; i < n; i++) {
    
    
			h[i] = sc.nextInt();
			w[i] = sc.nextInt();
			if(h[i] > w[i]) {
    
    
				int temp = h[i];
				h[i] = w[i];
				w[i] = temp;
			}
			if(h[i] > maxh) maxh = h[i];
			if(w[i] > maxw) maxw = w[i];	
		}
		int max = maxw < maxh ? maxw : maxh;
		
		while(max >= 1) {
    
    
			int count = 0;
			for (int i = 0; i < n; i++) {
    
    
				count += (h[i]/max)*(w[i]/max);
			}
			if(count >= k) break;
			max--;
		}
		System.out.println(max);
		sc.close();
	}
}

Guess you like

Origin blog.csdn.net/L333333333/article/details/105148137