20200617华为笔试题

#include 
#include 
#include 
#include 
using namespace std;

int main()
{
	string str;
	int n;

	while (getline(cin, str))
	{
		cin >> n;
		cin.ignore();
		int len = str.size();
		vector input;
		stringstream ss(str);
		string tmp;
		while (getline(ss, tmp, ' ')) input.push_back(tmp);
		vector vec(1, "");
		int line = 0;
		char c;
		int index = 0;
		for (int i = 0; i < input.size(); i++)
		{
			tmp = input[i];
			int tlen = tmp.size();
			while (index + tlen > n)
			{
				   line = vec.size() - 1;
					vec[line] += tmp.substr(0, n-index-1);
					vec[line] += '-';
					vec.push_back("");
					tlen = tlen - (n-index-1);
					tmp = tmp.substr(n-index-1);
			}
			   line = vec.size() - 1;
				vec[line] += tmp;
				if (index + tlen == n-1)
				{
					if (i != input.size() - 1) vec[line] += ' ';
					vec.push_back("");
					index = 0;
				}
				else if (index + tlen == n - 2)
				{
					vec.push_back("");
					index = 0;
				}
				else
				{
					if (index + tlen == n)
					{
						index = 0;
						vec.push_back("");
					}

					else
					{
						if (i != input.size() - 1) vec[line] += ' ';
						index += tlen;
					}
				}
		}
		for (int i = 0; i < vec.size(); i++)
		{
			if (vec[i] != "") cout << "{" << vec[i] << "}" << endl;
		}
	}
	return 0;
}

 

你可能感兴趣的:(20200617华为笔试题)