C语言编写函数,以参数方式返回n个整数的最大值、最小值、大于平均值的数据的个数。 输入格式: 输入数据的第一行为一个正整数T, 表示测试数据的组数。每组测试数据先输入数据个数n,然后输入n个整数。 输出格式: 对于每组测试,在一行上输出最大值、最小值、大于平均值的数据的个数。数据之间以1个空格分隔。 输入样例: 2 4 1 2 5 2 5 4 3 5 1 2 输出样例: 5 1 1 5 1 2
时间: 2024-03-12 11:48:14 浏览: 87
下面是这个问题的 C 语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
void getMaxMinAndCount(int *arr, int n, int *max, int *min, int *count) {
int sum = 0;
*max = *min = arr[0];
for (int i = 0; i < n; i++) {
if (arr[i] > *max) {
*max = arr[i];
}
if (arr[i] < *min) {
*min = arr[i];
}
sum += arr[i];
}
double avg = (double)sum / n;
*count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] > avg) {
(*count)++;
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int max, min, count;
getMaxMinAndCount(arr, n, &max, &min, &count);
printf("%d %d %d\n", max, min, count);
free(arr);
}
return 0;
}
```
函数 `getMaxMinAndCount` 接收一个整数数组和数组长度,以及三个指针变量 `max`、`min` 和 `count`,通过遍历数组获取最大值、最小值和大于平均值的数据的个数。主函数中按照题目要求输入数据并输出结果,注意每次循环后要释放动态分配的数组空间。
阅读全文
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)