The first written examination of Huawei interns in Chengdu and Chongqing in 2019

Keywords: Programming less

Two days ago, Huawei practiced computer problems in Chengdu and Chongqing. For the first time, it didn't check anything on the Internet. It was really compiled by itself. With the tension of the exam, it was really confused and exposed a lot of basic problems. Take the first question, C + +, to talk about my basic problems at that time. Pictures and ideas from ZY brothers

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
	/* erase Delete the first element, pop back delete the last element
    size()Refers to the number of elements currently owned by the container;
	capacity()The total number of elements that a container can store before it has to allocate storage space.*/
	int getnum,maxlen=0,i=0,count=0,num=0;
	cin >> getnum;
	string temp;
	vector<string> zq;
	while (cin >> temp)
		zq.push_back(temp);
	//Find the longest length of an array
	for (int i=0;i<zq.size();i++)
	{
		if (maxlen < zq[i].size())
			maxlen = zq[i].size();
	}
	//Count the number of polling arrays
	if (maxlen % (getnum * 2) == 0)
		count = maxlen / (getnum * 2);
	else
		count = maxlen / (getnum * 2) + 1;
	//Start output
	for (int k = 0; k < count; k++)//Poll times
	{
		for (int j = 0; j < zq.size(); j++)//Polling several arrays
		{
			if ((zq[j].size() < getnum * 2) && (zq[j].size() > 0))//If the number of array elements polled is less than getnum, output all
			{
				cout << zq[j];
				zq[j].erase(0, zq[j].size());//Delete element after output
				num++;
				if (num != zq.size())//Last output without comma
					cout << ',';
			}
			else if (zq[j].size() == 0)
				;
			else//If the number of array elements polled is enough, the corresponding output
			{
				for (int i = 0; i < getnum * 2; i++)
					cout << zq[j][i];
				zq[j].erase(0,getnum*2);//Delete element after output
			}
		}
	}
	cout << endl;
	system("pause");
	return 0;
}

Dozens of lines, two hours or all kinds of bugs. Add more fuel and brush questions on the computer.
1. Understand the insertion, deletion from the beginning and the end of container vector, as well as the size, capacity and other knowledge;
2. There should be a clear idea for the question, for example, the number of polling can be calculated for this question first;
3. For the understanding of circular input, I didn't think of while in the first exam, because I had long forgotten the end of file;
Remaining questions:
1. The title requires that the result be stored in a new array, and no longer be assigned;
2. In the example of the title, the end of Ctrl+Z file is not displayed. At that time, it was also because of this unexpected end of input.

The above code can only support one digit. If the array element is a multi digit number, I temporarily want to use comma as a flag, judge the number of elements, and then output. The optimized code is:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int getnum, elementnum[10] = {},maxnum=0,times=0,num=0,coutnum=0;
	string temp;
	cin >> getnum;
	vector<string> zq;
	while (cin >> temp)
		zq.push_back(temp);
	for (int i = 0; i < zq.size(); i++)
	{
			elementnum[i] = count(zq[i].begin(), zq[i].end(), ',') + 1;
		if (maxnum < elementnum[i])
			maxnum = elementnum[i];//Number of elements is comma + 1
	}
	//Count the number of polling arrays
	if (maxnum % getnum  == 0)
		times = maxnum / getnum ;
	else
		times = maxnum / getnum + 1;
	//Start output
	for (int k = 0; k < times; k++)//Poll times
	{
		for (int j = 0; j < zq.size(); j++)//Polling several arrays
		{
			if ((elementnum[j] <= getnum) && (elementnum[j] > 0))//If the number of array elements polled is less than getnum, output all
			{
				cout << zq[j];
				zq[j].erase(0, zq[j].size());//Delete element after output
				if (zq[j].size() > 0)
					elementnum[j] = count(zq[j].begin(), zq[j].end(), ',') + 1;//Update number of array elements
				else
					elementnum[j] = 0;
				num++;
				if (num < zq.size())//Last output without comma
					cout << ',';
			}
			else if (elementnum[j] == 0)
				;
			else//If the number of array elements polled is enough, the corresponding output
			{
				int size = zq[j].size();
				for (int commanum=0; coutnum < size; coutnum++)
				{
					cout << zq[j][coutnum];
					if (zq[j][coutnum] == ',')
						commanum++;
					if (commanum == getnum)
						break;
				}
				zq[j].erase(0, coutnum+1);//Delete element after output
				elementnum[j] = count(zq[j].begin(), zq[j].end(), ',') + 1;//Update number of array elements
				coutnum = 0;
			}
		}
	}
	cout << endl;
	system("pause");
	return 0;
}

Posted by Dark.Munk on Sat, 23 Nov 2019 12:00:03 -0800