[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
Sample Output
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.
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; }
相关文章推荐
- 聚合函数:sum,avg,max,min,count;模糊查询;排序
- Linq——Count、Sum、Min、Max、Average
- Oracle分析函数三——SUM,AVG,MIN,MAX,COUNT
- LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg - YJingLee's Blog - 博客园(转)
- linq语法之select distinct Count Sum Min Max Avg
- C#奇妙的函数(1)-五个序列聚合运算(Sum, Average, Min, Max,Aggregate)
- oracle学习笔记(二)substr,nvt,max,min,avg,sum,count,group,having
- LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg
- Linq学习笔记(1.8)——Count、Sum、Min、Max、Average
- Linq——Count、Sum、Min、Max、Average
- SQL---DML---聚集函数AVG(),COUNT(),SUM(),MAX(),MIN()的使用
- oracle中的聚合函数count、max、min、sum、avg等等
- LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg
- RX操作符之算术和聚合操作(averageInteger、min、max、count、sum、contact、reduce、collect)
- MySQL学习足迹记录10--汇总数据--MAX(),MIN(),AVG(),SUM(),COUNT()
- 步步为营VS 2008 + .NET 3.5(7) - LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault、ElementAt、ElementAtOrDefault、Contains、Any、All、Count、LongCount、Sum、Min、Max、Average、Aggregate、Cast、DefaultIfEmpty、SequenceEqual、OfType、ToArray、ToList、ToDictionary
- [导入]Linq学习笔记(1.8)——Count、Sum、Min、Max、Average
- LINQ体验(5)——LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg
- LINQ体验(5)——LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg
- Hive分析窗口函数(一) SUM,AVG,MIN,MAX