您的位置:首页 > 其它

[Hackerrank]Max min sum

2017-07-24 09:24 417 查看
Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

Input Format

A single line of five space-separated integers.

Constraints

Each integer is in the inclusive range .

Output Format

Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of
the five integers. (The output can be greater than 32 bit integer.)

Sample Input

1 2 3 4 5


Sample Output

10 14


Explanation

Our initial numbers are , , , ,
and .
We can calculate the following sums using four of the five integers:

If we sum everything except ,
our sum is .

If we sum everything except ,
our sum is .

If we sum everything except ,
our sum is .

If we sum everything except ,
our sum is .

If we sum everything except ,
our sum is .

As you can see, the minimal sum is  and
the maximal sum is .
Thus, we print these minimal and maximal sums as two space-separated integers on a new line.

Hints: Beware of integer overflow!
Use 64-bit Integer.

#include <iostream>
using namespace std;

int main() {
long long arr[5];
long long max=0,sum=0,min;
for(int arr_i = 0; arr_i < 5; arr_i++){
cin >> arr[arr_i];
}
min = arr[0];
for(int i=0;i<5;i++)
{
if(arr[i]>max)  max = arr[i];
if(arr[i]<min)  min = arr[i];
sum += arr[i];
}
max = sum-max;
min = sum-min;
cout<<max<<' '<<min;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐