题目链接:公务员面试_牛客题霸_牛客网 (nowcoder.com)
思路:首先看见题目要去掉头尾,我们会思考如何去掉,此时简单的思路就是先找到在拿掉;
在把剩余数字相加求平均值。
法一:
#include<stdio.h>
int main()
{
int i = 0;
int j = 0;
int arr[7] ={0};
int max = 0;
float sum = 0.0;
int min = 100;
int size = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < size; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
if (arr[i] < min)
{
min = arr[i];
}
}
for (j = 0; j < size; j++)
{
sum += arr[j];
}
printf("%.2f", (sum - min - max)*1.0 / (size - 2));
return 0;
}
法二:
#include <stdio.h>
int main()
{
int i = 0;
int sum = 0;
int min = 100;//假设最小值是100
int max = 0;//假设最大值是0
int score = 0;
for(i=0; i<7; i++)
{
scanf("%d ", &score);
sum += score;
if(score > max)
max = score;
if(score < min)
min = score;
}
printf("%.2f\n", (sum-min-max)/5.0);
return 0;
}
|