309. 中位数-计算机二2014

#include 
#include 
#include  
#include 
#include 
using namespace std;
int nums[100]; 
string int2string(int i) {
	stringstream stream;
	stream << i;
	return stream.str();
}
int main(int argc, char** argv) {
	int T;
	scanf("%d", &T);
	while (T--) {
		int N;
		scanf("%d", &N);
		for (int i = 0; i < N; ++i) scanf("%d", &nums[i]);
		sort(nums, nums + N);
		if (N % 2 == 0) {
			float mid = (nums[N / 2 - 1] + nums[N / 2]) / 2.0;
			int zeros = 0;
			int midInt = (int)mid;
			while (mid > midInt) {
				++zeros;
				mid *= 10;
				midInt = (int)mid;
			}
			string format = "%." + int2string(zeros) + "f\n";
			printf(format.c_str(), (nums[N / 2 - 1] + nums[N / 2]) / 2.0);
		} // 以上可以用printf("%g\n", (nums[N / 2 - 1] + nums[N / 2]) / 2.0);来替换 %g:Use the shortest representation
		else printf("%d\n", nums[(N - 1) / 2]);
	}
	return 0;
}

你可能感兴趣的:(BUPT,Excited,OJ)