1. 程式人生 > >2018年百度秋季招聘C++開發工程師筆試程式設計題一

2018年百度秋季招聘C++開發工程師筆試程式設計題一

題意為從一個亂序陣列中,將其中的整數按照出現的頻次多少來排列(並且出現幾次就排列幾個),比如輸入為[1,2,1,2,3,3,1,6,4,4,4,4],那麼輸出就應該為[4,4,4,4,1,1,1,2,2,3,3,6],其中,如果某兩個數字的出現頻次相同,那麼就按照輸入用例中的原順序排列。解題程式碼如下:

int get_max_num_index(int* array, int length)
{
	int temp = 0, max_num = array[0];
	for (int i = 1; i < length; i++)
	{
		if (array[i] > max_num)
		{
			max_num = array[i];
			temp = i;
		}
	}
	return temp;
}

vector<int> num_frequency_output(int* numbers, int num)
{
	int* num_frequency = new int[num];
	int count = 0;
	vector<int> res;
	for (int i = 0; i < num; i++)
	{
		num_frequency[i] = -1;
	}
	for (int i = 0; i < num; i++)
	{
		count = 1;
		if (num_frequency[i] == 0) { continue; }
		for (int j = i + 1; j < num; j++)
		{
			if (numbers[j] == numbers[i])
			{
				num_frequency[j] = 0;
				count++;
			}
		}
		num_frequency[i] = count;
	}
	for (int i = 0; i < num; i++)
	{
		int cur_max_frequen_index = get_max_num_index(num_frequency,num);
		if (num_frequency[cur_max_frequen_index] != 0)
		{
			for (int j = 0; j < num_frequencyp[cur_max_frequen_index]; j++)
			{
				res.push_back(numbers[cur_max_frequen_index]);
			}
		}
		for (int k = 0; k < num; k++)
		{
			if (num_frequency[k] == num_frequency[cur_max_frequen_index])
			{
				num_frequency = 0;
			}
		}
	}

}