题目描述
Write a program which reads a sequence of n integers ai(i=1,2,…n), and prints the minimum value, maximum value and sum of the sequence.
输入
In the first line, an integer n is given. In the next line, n integers ai are given in a line.
输出
Print the minimum value, maximum value and sum in a line. Put a single space between the values. [alert title=”注:” icon=”flag” color=”green”]还应该比较max或min与新赋值i的大小[/alert]
题解
#include<bits/stdc++.h>
using namespace std;
int main() {
int a, n, i, max, min, sum, t;
scanf(“%d %d”, &n, &a);
sum = a;
max = a; min = a;
while (n > 1) {
scanf(“%d”, &i);
sum = sum + i;
if (a <= i) {
if (max < i) {
max = i;
}
if (min > a) {
min = a;
}
}
else {
if (max < a) {
max = a;
}
if (min > i) {
min = i;
}
}
n = n - 1;
}
cout << min << “ “ << max << “ “ << sum << endl;
return 0;
}