题意:
输入N个数,去掉最大和最小的数,求剩余的数的平均数。
解法:
找到分别最大和最小的数,然后从总和中减去他们,再求平均数(不要排序);
1: #include<stdlib.h>
2: #include<stdio.h>
3: #include<string.h>
4: #include<math.h>
5: #define INF 0x3f3f3f3f
6: int main(){
7: int n,i;
8: while(scanf("%d",&n)!=EOF){
9: int max=0,min=INF,sum=0,tmp;
10: for(i=0;i<n;i++){
11: scanf("%d",&tmp);
12: sum+=tmp;
13: if(max<=tmp)
14: max=tmp;
15: if(min>=tmp)
16: min=tmp;
17: }
18: sum=sum-max-min;
19: printf("%.2lf\n",sum*1.0/(n-2));
20: }
21: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }