题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2014
要实现的算法是:求整个数组的和、在数组中找最值。
找最值,可以先把第一个元素赋给max、min变量,做一次遍历,一一比较,把最大值存入max,最小值存入min。
也可以直接对数组进行排序,然后从第二个加到倒数第二个,这样就可以了,省去减两个最值。
编码建议
我的代码采用的是第一种方法。因为对于本题来说,它的效率是O(N)。 算法不难,可以直接看代码 这里稍微讨论一下第二种方法。 语法: #include <stdlib.h> 功能: 对buf 指向的数据(包含num 项,每项的大小为size)进行快速排序。如果函数compare 的第一个参数小于第二个参数,返回负值;如果等于返回零值;如果大于返回正值。函数对buf 指向的数据按升序排序。 你可能有疑问,为什么cmp函数不直接用return *a - *b;#include <stdio h="">#include <stdlib h="">int cmp(const double *a, const double *b){return *a > *b ? 1 : *a < *b ? -1 : 0;}int main(void){int n, i;double x, y[100];while (scanf("%d", &n) != EOF){for (i = 0 ; i < n ; i++)scanf("%lf", y + i);qsort(y, n, sizeof(double), cmp);for (x = 0, i = 1 ; i < n - 1 ; i++)x += y[i];printf("%.2f\n", x / (n - 2));}return 0;}</stdlib></stdio> 这也就是我不打算用这种方法的原因了(虽然上面的代码可以Accpted),这就是这段代码的不稳定因素。 我们来做个实验: 上面的代码的作用就是确定X小数点后有几位。#include <math h="">#include <stdio h="">int main(void){int i;double x;x = 0.123456;printf("%lf\n", x);for (i = 0 ; x - floor(x); i++)x *= 10;printf("%d\n", i);return 0;}</stdio></math> 我相信所有人都知道它有6位小数。但它的运行结果却是17(因编译器而异)。 你可以自己复制代码去验证一下。 我们在循环体里插入printf("%lf\n", floor(x));看看它的变化是怎样的。 运行结果: 这就是double型数据的精度问题。这是我们无法人为地控制的。所以建议尽量避免double的高精度运算。 |
#include <stdio.h>
int main(void)
{
int n, i;
double min, max;
double x, y;
while (scanf("%d", &n) != EOF)
{
scanf("%lf", &x);
min = max = x;
for (i = 1 ; i < n ; i++)
{
scanf("%lf", &y);
x += y;
if (y > max) max = y;
if (y < min) min = y;
}
printf("%.2lf\n", (x - min - max) / (n - 2));
}
return 0;
}