码迷,mamicode.com
首页 > 其他好文 > 详细

uva 10107 - What is the Median?

时间:2016-01-02 14:16:47      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

 

 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 long long Median, arr[10010];
 6 
 7 
 8 int main()
 9 {
10     int i, cur_index, count, isOdd;
11     count = 0;
12     while(scanf("%lld", &arr[0]) != EOF)    // arr[0] is a monitor
13     {
14         count ++;    // count: caculate numbers
15         cur_index = count;    // cur_index initial value: count
16         for(i=1; i<count; i++)
17         {
18             if(arr[0] < arr[i])
19             {
20                 cur_index = i;
21                 break;
22             }
23         }
24 
25         for(i=count; i>cur_index; i--)    // backword move to arr[count]
26             arr[i] = arr[i-1];
27         
28         arr[cur_index] = arr[0];    // whatever, this is must.
29 
30         isOdd = count % 2;
31         if(isOdd)
32             Median = arr[count/2 + 1];    // odd
33         else Median = (arr[count/2 +1] + arr[count/2]) / 2;    // even
34 
35         cout << Median << endl;
36     }
37     return 0;   
38 }

 

犯错:漏写 break; 语句

 

uva 10107 - What is the Median?

标签:

原文地址:http://www.cnblogs.com/aze-003/p/5094542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!