SPOJ - TEAM2 A Famous ICPC Team

Description

Mr. B, Mr. G, Mr. M and their coach Professor S are planning their way to Warsaw for the ACM-ICPC World Finals. Each of the four has a square-shaped suitcase with side length Ai (1 <= i <= 4) respectively. They want to pack their suitcases into a large square box. The heights of the large box as well as the four suitcases are exactly the same. So they only need to consider the large box’s side length. Of course, you should write a program to output the minimum side length of the large box so that the four suitcases can be put into the box without overlapping.

Input

Each test case contains only one line containing 4 integers Ai (1<= i <=4, 1<= Ai <=1,000,000,000) indicating the side length of each suitcase.

Output

For each test case, display a single line containing the case number and the minimum side length of the large box required.

Example

Input:
2 2 2 2
2 2 2 1

Output:
Case 1: 4
Case 2: 4

Explanation

For the first case, all suitcases have size 2x2. So they can perfectly be packed in a 4x4 large box without wasting any space.

For the second case, three suitcases have size 2x2 and the last one is 1x1. No matter how to rotate or move, you could find the side length of the box must be at least 4.

【题目大意】B先生,G先生,M先生和他们的教练S正计划前往华沙参加ACM-ICPC世界总决赛。他们四个中的每一个都有一个方形手提箱,边长分别为A_iAi​(1 <= i <= 4)。 他们想把所有的行李箱装进一个大方箱里。 大箱子和四个手提箱的高度完全相同。 所以他们只需要考虑大箱子的边长。 编写一个程序来输出大盒子的最小边长,这样四个手提箱就可以放入盒子而不会重叠。 

【题解】 

只要两个最大的小方箱放在一起(不论竖着摆还是横着摆),那么其余的两个小方箱放置时,边长一定不会超过这两个最大小方箱的边长(也不论竖着摆还是横着摆) 。

至于怎样找到边长第一和第二大的小方箱,我们可以用数组输入,最后一个sort就行了。

#include <stdio.h>
#include <algorithm>

using namespace std;

int main()
{
    int a[5];
    int t = 1;
    while (~scanf("%d %d %d %d", &a[0], &a[1], &a[2], &a[3])){
        sort(a, a+4);
        printf("Case %d: %d\n", t++, a[2]+a[3]);
    }
        
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Aibiabcheng/article/details/105480425