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; 语句
1 // 20160125 2 #include <cstdio> 3 #include <iostream> 4 using namespace std; 5 6 int main() 7 { 8 int x, i, j, arr[10005], n = 0; 9 while(scanf("%d", &x) != EOF) 10 { 11 j = n; 12 for(i = 0; i < n; i++) 13 if(x < arr[i]) 14 { 15 j = i; 16 break; 17 } 18 for(i=n; i > j; i--) 19 arr[i] = arr[i-1]; 20 arr[j] = x; 21 n ++; 22 23 if(n % 2) cout << arr[n / 2] << endl; 24 else { 25 long median = 0; 26 median = median + arr[n / 2 - 1] + arr[n / 2]; 27 cout << median / 2 << endl; 28 } 29 } 30 return 0; 31 }