[Pointers and C-String]D. Liang 7.1 Analyzing input
Description:
Use pointers on array to write a program that reads n numbers, computes their average, and finds out how many numbers are above the average.
Input:
The first line is a positive integer t for the number of test cases.
Each test case contains two lines. The first line is an integer n (0<n<=100). The second line contains n numbers.
Output:
For each test case, output how many numbers above the average in one line.
Sample Input:
Copy sample input to clipboard2 3 2 1.5 2.5 4 2 -6 2 6
Sample Output:
1 3
Programme:
//Date:2020/5/7
//Author:Kamenrider Justice
#include<stdio.h>
int main()
{
int t,i;
int compar(double* p,double ave,int size);//比较有几个比平均值大
scanf("%d",&t);
for(i=0;i<t;i++)
{
int n=0,j,count;
double ave=0,sum=0;
scanf("%d",&n);
double num[n];
for(j=0;j<n;j++)
{
scanf("%lf ",&num[j]);
sum+=num[j];//求和
}
ave=sum/n;//求平均值
count=compar(num,ave,n);//求出有几个比平均值大
printf("%d\n",count);
}
return 0;
}
int compar(double* p,double ave,int size)
{
int i,count=0;
for(i=0;i<size;i++)
{
if(*(p+i)>ave)
{
count++;
}
}
return count;
}
Java面试Offer直通车