Exercise 5-1 uva593 Code alignment (the last word with no spaces!)

This question is to open a string on nothing of
how it is aligned, open array, save the word length of each column maximum
output with automatic filled spaces function (hand-written ah) has greatly Liu used the silence to remember -
As long as the ranks of chaos have to say
on the code

#include <bits/stdc++.h>
using namespace std;
string str[1005][200];
int    len[1005][200];
int maxn[200],chang[1005];
void print(string s, int n, char c) //自动补空格 
{
	int l = s.size();
	for (int i = 0; i < n ;i++)
	{
		if(i < l) cout<<s[i];
		else cout<<c;
	}
}
int main()
{
	string s;
	int n = 0, MA = 0;
	while(getline(cin,s))
	{
		int z = 0;
		stringstream ss(s);  // 读每行单词专用流 
		while(ss >> str[n][z])
		{
			len[n][z] = str[n][z].size(); //单词长度	
			z++;
		}
		chang[n++] = z;  //单词个数 
		MA = max(z,MA);
	}
	for (int i = 0; i < MA; i++) //最多的单词个数
	{
		for (int j = 0; j < n; j++)
		{
			if(i >= chang[j]) continue; // 超过其行的单词个数就算了 
			else maxn[i] = max(maxn[i],len[j][i]); 
		}
	} 
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < chang[i]; j++)
		{
			if(j) printf(" ");
			if(j != chang[i]-1) print(str[i][j],maxn[j],' ');
			else cout<<str[i][j];
		}
		printf("\n");
	} 
	  return 0;
} 
Published 55 original articles · won praise 1 · views 2661

Guess you like

Origin blog.csdn.net/qq_37548017/article/details/100053031