10057

不难想,就是求中位数,但是一直没过是因为要求输出的第三个东西没大看明白。
我以为偶数情况中间两数不同的话要输出2,看了别人的解题报告才明白要输出的是这两个数之间有多少个数。真费解。。
//============================================================================
// Name        : 10057.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int a[1000010];
long long sum1, sum2;
int num1, num2, t1, t2, n;

int main() {
	freopen("a.txt", "r", stdin);
	while(scanf("%d", &n)!=EOF){
		//memset(a, 0, sizeof(a));
		for(int i = 0;i < n;i++){
			scanf("%d", &a[i]);
		}
		sort(a, a+n);
		if(n%2){
			t1 = n/2;
			num1 = 0;
			for(int i = 0;i < n;i++){
				if(a[i] == a[t1]){
					num1++;
				}
			}
			printf("%d %d 1\n", a[t1], num1);
		}
		else{
			t1 = n/2-1;
			t2 = n/2;
			num1 = 0;
			num2 = 0;
			sum1 = 0;
			sum2 = 0;
			for(int i = 0;i < n;i++){
				if(a[i] == a[t1]){
					num1++;
				}
				if(a[i] == a[t2]){
					num2++;
				}
				sum1 += (a[i]-a[t1] >= 0?a[i]-a[t1]:a[t1]-a[i]);
				sum2 += (a[i]-a[t2] >= 0?a[i]-a[t2]:a[t2]-a[i]);
			}
			if(a[t1] == a[t2]){
				printf("%d %d 1\n", a[t1], num1);
			}
			else{
				printf("%d %d %d\n", a[t1], num1+num2, a[t2]-a[t1]+1);
			}
		}
	}
	return 0;
}
posted @ 2011-05-22 10:38  KOKO's  阅读(347)  评论(0编辑  收藏  举报