解題心得
很像高中數學曾經學過的題目,印象中解法是:
1. 當 n 為奇數,則答案為中位數
2. 當 n 為偶數,則答案為區間內的所有整數
程式碼
#include <iostream>
#include <algorithm>
using namespace std;
int n, arr[1000000] = { 0 };
int main()
{
	while (cin >> n)
	{
		for (int i = 0; i < n; i++)
			cin >> arr[i];
		sort(arr, arr + n);
		if (n % 2 == 1)
			cout << "A=" << arr[n / 2] << endl;
		else
		{
			cout << "A=";
			for (int i = arr[n / 2 - 1]; i <= arr[n / 2]; i++)
			{
				if (i != arr[n / 2])
					cout << i << "、";
				else
					cout << i << endl;
			}
		}
		
	}
	return 0;
}
 
沒有留言:
張貼留言