Input 10 integers , find their average , and output the number of data that is greater than the average .
Output format
Number greater than average
Sample input
1 2 3 4 5 6 7 8 9 10
Sample output
5
This question is simple, just create an array and enter 10 numbers . Calculate the cumulative sum and then find the average , and then the for loop traverses the array data and compares it with the average . If it is larger, it is cnt+1. If it is not larger, there is no operation. Finally, just output the number directly.
//给10个数 求平均值然后有()个大于平均值的个数
#include<iostream>
using namespace std;
int main(){
int a[10]; //定义10个元素的数组
int sum=0;
for(int i=0;i<10;i++){ //录入10个数
cin>>a[i];
sum=sum+a[i]; //sum+=a[i]
}
sum=sum/10; //sum变成平均数
int cnt=0; //表示大于平均数的数
for(int i=0;i<10;i++){
if(a[i]>sum){
++cnt; //++cnt比cnt++快(不考虑先后顺序的情况下)
}
}
cout<<cnt<<endl; //输出超过平均数的个数
return 0;
}