蓝桥杯2020年真题:分配口罩

题目

本题总分:10 分
【问题描述】
某市市长获得了若干批口罩,每一批口罩的数目如下:
(如果你把以下文 字复制到文本文件中,请务必检查复制的内容是否与文档中的一致。
在试题目 录下有一个文件 mask.txt,内容与下面的文本相同) 
9090400 
8499400 
5926800 
8547000 
4958200 
4422600 
5751200 
4175600 
6309600 
5865200 
6604400 
4635000 
10663400 
8087200 
4554000 

现在市长要把口罩分配给市内的 2 所医院。
由于物流限制,每一批口罩只 能全部分配给其中一家医院。
市长希望 2 所医院获得的口罩总数之差越小越好。 
请你计算这个差最小是多少?

【答案提交】
这是一道结果填空题,你只需要算出结果后提交即可。
本题的结果为一个 整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

答案

2400
package competition4;

public class Distribution2
{
    
    
	public static int[] arr =
		{
    
     9090400, 8499400, 5926800, 8547000, 4958200, 4422600, 5751200, 
				4175600, 6309600, 5865200, 6604400, 4635000,
				10663400, 8087200, 4554000 };
	
	public static int min=Integer.MAX_VALUE;
	public static int sum=0;
	public static boolean[] visited=new boolean[arr.length];
	
	public static void main(String[] args)
	{
    
    
		for(int x=0;x<arr.length;x++)
		{
    
    
			sum +=arr[x];
		}
		dfs(0);
		System.out.println(min);
	}
	public static void dfs(int index)
	{
    
    
		if(index>=sum/2)
		{
    
    
			min = Math.min(min, Math.abs(sum-index*2));
			return;
		}
		for(int x=0;x<arr.length;x++)
		{
    
    
			if(!visited[x])
			{
    
    
				visited[x]=true;
				dfs(index+arr[x]);
				visited[x]=false;
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_43416157/article/details/108975703