HUAWEI OD Machine Test - Minimum Sum of Equal and Subarrays - 2022Q4 Volume A - Py/Java/JS

Given an array nums, divide the elements into several groups so that the sums of each group are equal, and find the minimum value of the sum of elements in the group among all groups that meet the conditions

Input description:
Enter m in the first line
and then enter m numbers to represent this array
Data range: 1<=M<=50, 1<=nums[i]<=50

Output description:

Minimum split array sum.

Example:

enter:

7
4 3 2 3 5 2 1

output:

5

Explanation: The situations that can be equally divided are:

4 subsets (5), (1,4), (2,3), (2,3)

2 subsets (5, 1, 4), (2,3, 2,3)

But the minimum is 5.

java code

import java.util.Scanner;
import java.util.*;
 
class Main {
	public static void main(String[] args) {
        // 处理输入
        Scanner in = new Scanner(System.in);
        String param_str = in.nextLine();
        int count = Integer.valueOf(param_str);
 
        //构造输入数据结构,并求和
        int[] nums = new int[count];
        String num_str = in.nextLine();
 

Guess you like

Origin blog.csdn.net/miao_9/article/details/130213162