CCF201903-1小中大

 

思路:

  首先n的范围为10^5,定义在函数内部不会导致栈溢出,但是建议定义全局数组。有8个0,否则系统会判你运行错误。有序的数,可能是倒序也可能是正序,可以直接用sort进行排序。此题唯一需要考究的是判断分数并且保留小数。可以用奇数偶数进行判断,对于n是奇数的情况中位数直接为n/2,n是偶数的情况,中位数为中间两数的平均数,即n/2和n/2-1。如果两数之和为偶数,中位数肯定是整数,如果是奇数,取半再加0.5即可。还有一种思路是直接用double类型得到两数的平均数,在通过判断有没有小数部分即可。即zhong-(int)zhong == 0

#include
#include
using namespace std;
const int N = 100000;
int shu[N];
int main(void) {
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &shu[i]);
	}
	sort(shu, shu + n);
	if (n % 2 == 1) {
		printf("%d %d %d\n", shu[n - 1], shu[n / 2], shu[0]);
	}
	else  {
		int zhong = shu[n / 2] + shu[n / 2 - 1] ;
		if (zhong % 2 == 0) {
			printf("%d %d %d\n", shu[n - 1], zhong / 2, shu[0]);
		}
		else {
			zhong /= 2;
			printf("%d %.1f %d\n", shu[n - 1], (double)zhong + 0.5, shu[0]);
		}
	}
	return 0;
	

}

 

你可能感兴趣的:(CSP)