uva 10107 - What is the Median?

时间:2022-10-27 11:17:32
 #include <cstdio>
#include <iostream>
using namespace std; long long Median, arr[]; int main()
{
int i, cur_index, count, isOdd;
count = ;
while(scanf("%lld", &arr[]) != EOF) // arr[0] is a monitor
{
count ++; // count: caculate numbers
cur_index = count; // cur_index initial value: count
for(i=; i<count; i++)
{
if(arr[] < arr[i])
{
cur_index = i;
break;
}
} for(i=count; i>cur_index; i--) // backword move to arr[count]
arr[i] = arr[i-]; arr[cur_index] = arr[]; // whatever, this is must. isOdd = count % ;
if(isOdd)
Median = arr[count/ + ]; // odd
else Median = (arr[count/ +] + arr[count/]) / ; // even cout << Median << endl;
}
return ;
}

犯错:漏写 break; 语句

 //
#include <cstdio>
#include <iostream>
using namespace std; int main()
{
int x, i, j, arr[], n = ;
while(scanf("%d", &x) != EOF)
{
j = n;
for(i = ; i < n; i++)
if(x < arr[i])
{
j = i;
break;
}
for(i=n; i > j; i--)
arr[i] = arr[i-];
arr[j] = x;
n ++; if(n % ) cout << arr[n / ] << endl;
else {
long median = ;
median = median + arr[n / - ] + arr[n / ];
cout << median / << endl;
}
}
return ;
}