# 问题引入

``````2 3 4 5 6 2 4 6 4 3
``````

``````3.833333
``````

# 理解1

## 去除一个最大/最小值

• 需要找到数组中的最大值和最小值
• 在计算平均值的时候需要剔除这两个值

``````int sp1(int arr[], int sz, int* pm)
{
int i = 0;
for (i = 0; i < sz; i++)
{
if (arr[i] > *pm)//找出最大值
{
*pm = arr[i];
}
}
for (int k = 0; k < sz; k++)
{
if (arr[k] == *pm)//确认最大值的数值
{
return k;//返回该最大值的下标
}
}
}
``````

``````int main()
{
int arr[10] = { 0 };
int a, b = 0;
double ave,sum= 0;
for (int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}

int sz = sizeof(arr) / sizeof(arr[0]);
a =sp1(arr, sz, &max);
b =sp2(arr, sz, &min);

for (int k = 0; k < 10; k++)
{
if (k != a && k != b)
{
sum = sum + arr[k];
}
}
ave = sum / 8;
printf("%lfn", sum);
printf("%lfn", ave);
return 0;
}
``````

# 题目实际要求

``````2 3 4 5 6 2 4 6 4 3
``````

## 解题思路3 -冒泡排序

### 计数器

``````int count2 = 1;
for (int k = 8; k >0 ; k--)
{
if (arr[k] == arr[9])
{
arr[k] = 0;//令最大值为0
count2 ++;
}
else
{
break;
}
}

``````

# 主函数代码

``````int main()
{
int arr[10] = { 0 };
int a, b = 0;
double ave, sum = 0;
for (int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
int sz = sizeof(arr) / sizeof(arr[0]);
//先对数组进行冒泡排序
bubble_sort(arr, sz);

//此时下标0和9即为最大最小值
int max = arr[0];
int min = arr[9];

int count1 = 1;
int count2 = 1;
for (int j = 1; j < 10; j++)
{
if (arr[j] == arr[0])
{
arr[j] = 0;//令最小值为0
count1 ++;
}
else
{
break;
}
}
for (int k = 8; k >0 ; k--)
{
if (arr[k] == arr[9])
{
arr[k] = 0;//令最大值为0
count2 ++;
}
else
{
break;
}
}
//令前后两个最大最小值为0
arr[0] = 0;
arr[9] = 0;

for (int k = 0; k < 10; k++)
{
sum = sum + arr[k];
}
//除去已知的所有最大最小值，计算平均数
ave = sum / (10- count1- count2);

//printf("%lfn", sum);
printf("%lfn", ave);
return 0;
}

``````

THE END