当前位置:首页 > 芯闻号 > 充电吧
[导读]要实现的算法是:求整个数组的和、在数组中找最值。找最值,可以先把第一个元素赋给max、min变量,做一次遍历,一一比较,把最大值存入max,最小值存入min。也可以直接对数组进行排序,然后从第二个加到

要实现的算法是:求整个数组的和、在数组中找最值。

找最值,可以先把第一个元素赋给max、min变量,做一次遍历,一一比较,把最大值存入max,最小值存入min。
也可以直接对数组进行排序,然后从第二个加到倒数第二个,这样就可以了,省去减两个最值。  


编码建议 Programing   我的代码采用的是第一种方法。因为对于本题来说,它的效率是O(N)。
算法不难,可以直接看代码

这里稍微讨论一下第二种方法。
对于排序,我们可以直接调用库函数qsort();

语法:

#include

功能: 对buf 指向的数据(包含num 项,每项的大小为size)进行快速排序。如果函数compare 的第一个参数小于第二个参数,返回负值;如果等于返回零值;如果大于返回正值。函数对buf 指向的数据按升序排序。


#include#includeint 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("%.2fn", x / (n - 2));}return 0;}

你可能有疑问,为什么cmp函数不直接用return *a - *b;
这也就是我不打算用这种方法的原因了(虽然上面的代码可以Accpted),这就是这段代码的不稳定因素。
我们来做个实验:

#include#includeint main(void){int i;double x;x = 0.123456;printf("%lfn", x);for (i = 0 ; x - floor(x); i++)x *= 10;printf("%dn", i);return 0;}

上面的代码的作用就是确定X小数点后有几位。
我相信所有人都知道它有6位小数。但它的运行结果却是17(因编译器而异)。
你可以自己复制代码去验证一下。
我们在循环体里插入printf("%lfn", floor(x));看看它的变化是怎样的。

运行结果:
1.000000
12.000000
123.000000
1234.000000
12345.000000
123455.000000
1234559.000000
12345599.000000
123455999.000000
1234559999.000000
12345599999.000000
123455999999.000000
1234559999999.000000
12345599999999.000000
123455999999999.000000
1234559999999999.000000
12345599999999998.000000

这就是double型数据的精度问题。这是我们无法人为地控制的。所以建议尽量避免double的高精度运算。  




#includeint 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("%.2lfn", (x - min - max) / (n - 2));
    }

    return 0;
}




本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

题目链接:hdu 3062 题面: Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav

关键字: hdu 入门

题目链接:HDU 4355 题面: Party All the Time Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536

关键字: hdu 技巧

题目链接:HDU 4544 题面: 湫湫系列故事——消灭兔子 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768

关键字: hdu 贪心

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3911 题面: Black And White Time Limit: 9000/3000 MS (

关键字: hdu 线段树

题目链接:HDU 5754 题面: Life Winner Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/13

关键字: hdu 博弈

题目链接:HDU 2045 题面: 不容易系列之(3)—— LELE的RPG难题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 6

关键字: hdu 入门

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 题面: Chat Time Limit: 2000/1000 MS (Java/Others

关键字: hdu 区域赛

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1297 题面: Children’s Queue Time Limit: 2000/1000 MS

关键字: hdu 入门

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题面: Partial Tree Time Limit: 2000/1000 MS (Jav

关键字: hdu 动态规划

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 题面: Rescue Time Limit: 2000/1000 MS (Java/Oth

关键字: hdu 搜索
关闭
关闭