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 num_frequency_output(int* numbers, int num)
{
	int* num_frequency = new int[num];
	int count = 0;
	vector 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;
			}
		}
	}

}

 

你可能感兴趣的:(面试经验)