输入个数 n, 然后再输入 n 个整数,求所有整数的总和、最大值、最小值, 并求最大值和 最小值的出现的位置,如果值相同,则取最后的位置。
#include
using namespace std;
int main(){
//Enter the numbers
int n;
cout << "Enter how many numbers and all numbers";
cin >> n;
int num[n];
for(int i = 0; i < n; i++)
cin >> num[i];
//Output the minmum number, maxmum number and sum
int sum = 0;
int maxNum, minNum, minI, maxI;
maxNum = num[0];
minNum = num[0];
minI = 0;
maxI = 0;
for(int j = 0; J < n; j++){
sum += num[j];
if(minNum >= num[j]){
minNum = num[j];
minI = j;
}
if(maxNum <= num[j]){
maxNum = num[j];
maxI = j;
}
}
cout << "The sum of the numbers is " << sum;
cout << "The maxmum and the minnum of the numbers is " << maxNum << " and " << minNum;
cout << "The location of the maxmum and the minnum of the numbers is" << maxI << " and " << minI;
return 0;
}