2014年2月17日 星期一

UVa OJ - 10107 What is the Median?

The following program is my ACcepted code for UVA-10107 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃UAV 10107 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )

//This program is for UVA 10107 What is the Median?
//http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=13&page=show_problem&problem=1048


#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
    vector<int> n;
    int tmp, count = 1;
 
    n.clear();
    n.push_back(0);//for the first zero index
 
    while(scanf("%d", &tmp)!=EOF)
    {
        n.push_back(tmp);

        sort(n.begin()+1, n.end());

        /* //for check
        for(int i=0 ; i<= count; i++)
            printf("%d ", n[i]);
        printf("\nmid: ");
        */
     
        if(count % 2)
            printf("%d\n", n[(count+1)/2]);
        else
            printf("%d\n", (n[count/2] + n[count/2 + 1])/2 );
     
        count ++;
    }
    return 0;
}

Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D 

歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )

沒有留言:

張貼留言

請留下您的任何想法或建議!
Please leave any thought or comment!