https://uva.onlinejudge.org/external/101/10107.pdf
In first look the problems seems to be easy but someone will get confused seeing the sample input output but don't worry it's after all an easy problem . the problem statement says that ,
1 . you have to determine current median that means you have to determine the median dynamically for every input .
2 . median value is that which divides an array into two equal parts and it differs for even number and odd number so you have to think what you should do when determining even numbers median and when odd numbers median .
3. You can use build in Sort function or Insertion sort Algorithm here to sort the value then calculate the median .
4 . Just give two if else statement which indicates when even and when odd . that's it .
An accepted code for this problem is given below :
#include<bits/stdc++.h>
#define m 10003
using namespace std;
int main()
{
long long int i=1,j,k,s;
long long int arr[m]={0};
while(scanf("%lld",&arr[i])==1)
{
sort(arr,arr+(i+1));
if(i==1)
{
printf("%lld\n",arr[i]);
}
else if(i%2==0)
{
s=(arr[i/2]+arr[i/2+1])/2;
printf("%lld\n",s);
}
else if(i%2!=0)
{
s=arr[i/2+1];
printf("%lld\n",s);
}
i++;
}
return 0;
}
In first look the problems seems to be easy but someone will get confused seeing the sample input output but don't worry it's after all an easy problem . the problem statement says that ,
1 . you have to determine current median that means you have to determine the median dynamically for every input .
2 . median value is that which divides an array into two equal parts and it differs for even number and odd number so you have to think what you should do when determining even numbers median and when odd numbers median .
3. You can use build in Sort function or Insertion sort Algorithm here to sort the value then calculate the median .
4 . Just give two if else statement which indicates when even and when odd . that's it .
An accepted code for this problem is given below :
#include<bits/stdc++.h>
#define m 10003
using namespace std;
int main()
{
long long int i=1,j,k,s;
long long int arr[m]={0};
while(scanf("%lld",&arr[i])==1)
{
sort(arr,arr+(i+1));
if(i==1)
{
printf("%lld\n",arr[i]);
}
else if(i%2==0)
{
s=(arr[i/2]+arr[i/2+1])/2;
printf("%lld\n",s);
}
else if(i%2!=0)
{
s=arr[i/2+1];
printf("%lld\n",s);
}
i++;
}
return 0;
}
No comments:
Post a Comment