CodeForces 1230A Dawid and Bags of Candies模拟

  • Post author:
  • Post category:其他

大家觉得写还可以,可以点赞、收藏、关注一下吧!
也可以到我的个人博客参观一下,估计近几年都会一直更新!和我做个朋友吧!https://motongxue.cn


CodeForces 1230A Dawid and Bags of Candies模拟 🤗

题目描述

Dawid has four bags of candies. The i -th of them contains

a

i

a_i

ai candies. Also, Dawid has two friends. He wants to give each bag to one of his two friends. Is it possible to distribute the bags in such a way that each friend receives the same amount of candies in total?

Note, that you can’t keep bags for yourself or throw them away, each bag should be given to one of the friends.

输入格式

The only line contains four integer

a

1

a_1

a1,

a

2

a_2

a2 ,

a

3

a_3

a3and

a

4

a_4

a4(

1

a

i

100

1≤a_i ≤100

1ai100 ) — the numbers of candies in each bag.

输出格式

Output YES if it’s possible to give the bags to Dawid’s friends so that both friends receive the same amount of candies, or NO otherwise. Each character can be printed in any case (either uppercase or lowercase).

题意:

四个袋子里有糖果要分给两个人

一个袋子只能给一个人,可以一人得到多袋,不能自己保留或者打开

如果两个人分得的和相同则输出YES,反之输出NO

输入格式

四个整数1≤ai≤100

输出格式

YES或NO(大小写任意)

输入输出样例
输入 #1复制
1 7 11 5
输出 #1复制
YES
输入 #2复制
7 3 2 5
输出 #2复制
NO

说明/提示

In the first sample test, Dawid can give the first and the third bag to the first friend, and the second and the fourth bag to the second friend. This way, each friend will receive 1212 candies.

In the second sample test, it’s impossible to distribute the bags.

分析

这个题目一开始认为每个人两个背包固定的,后来才发现是一个人得到的背包数并不固定,所以,A可以得到1-3个背包,B则相对应能得到3-1个背包,题目就求解出来了

  1. 四个背包大小排序
  2. 进行if判断

代码

C

# include <cstdio>
# include <iostream>
# include <algorithm>
using namespace std;
const int N=10;
int a[N];//用数组存,排序方便

int main () {
	scanf ("%d%d%d%d",&a[1],&a[2],&a[3],&a[4]);
	sort (a+1,a+1+4); 
	if (a[1]+a[2]+a[3]==a[4]) puts("YES");
	else if (a[1]+a[4]==a[2]+a[3]) puts("YES");
	else if (a[1]+a[3]==a[2]+a[4]) puts("YES");
    //以上三种可行解
	else puts("NO");
    //如若都无法实现,则输出NO
	return 0;
}

Java

/*
 * @Author: motongxue
 * @Date: 2020-09-15 20:06:56
 * @LastEditors: motongxue
 * @LastEditTime: 2020-09-15 20:41:04
 * @Blog: https://motongxue.cn
 * @Description: file content
 */
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] a = new int[4];
        for(int i=0;i<4;i++)a[i] = sc.nextInt();
        Arrays.sort(a);

        if((a[0]+a[3])==(a[1]+a[2]))System.out.println("YES");
        else if(a[0]+a[2]==a[1]+a[3])System.out.println("YES");
        else if(a[0]+a[1]+a[2]==a[3])System.out.println("YES");
        else System.out.println("NO");

    }
}


2020年9月15日更

大家觉得写还可以,可以点赞、收藏、关注一下吧!
也可以到我的个人博客参观一下,估计近几年都会一直更新!和我做个朋友吧!https://motongxue.cn



版权声明:本文为CrazyMooo原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。