(2013-2)A Famous ICPC Team

问题描述:

Mr. B, Mr. G, Mr. M and their coach Professor S are planning their way 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:

There are N test cases. The first line is N.

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.

Sample Input:

2
2 2 2 2
2 2 2 1

Sample Input:

Case 1: 4
Case 2: 4

题意:

给出四个正方形边长,将这四个正方形放入一个大正方形中,求大正方形的最小边长

思路:

最小边长即为四条边中最大两个边之和(要想取得最小边长,必须有两个正方形相邻,这样最小边长就由最大的两条边来决定)

#include <cstdio>
#include <algorithm>
using namespace std;

int main(){
    int a[4];
    int n;
    scanf("%d", &n);
    int count = 0;
    while(n--){
        for(int i = 0; i < 4; i++){
            scanf("%d", &a[i]);
        }
        sort(a, a+4);
        printf("Case %d: %d\n",++count, a[2]+a[3]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_35093872/article/details/88086492