Alignment of Code
UVA - 1593
只给出题目链接;
题意:输入若干代码,要求各列单词的左边界对齐并且尽量靠左。单词之间至少要空一格。
每个单词不超过80字符,每行不超过180字符,一共最多1000行,样例如下:
Sample Input
start: integer; // begins here stop: integer; // ends here s: string; c: char; // temp
Sample Output
start: integer; // begins here stop: integer; // ends here s: string; c: char; // temp
思路看代码,比较简单。
#include<bits/stdc++.h>
using namespace std;
const int MAX = 1e3+5;
vector<string> w[MAX];
int h[200], line = 0;
int main() {
string s, s1;
while(getline(cin,s)) {
int cnt = 1;
line++;
stringstream ss(s);
while(ss >> s1) {
h[cnt] = max(h[cnt], (int)s1.length());
w[line].push_back(s1);
cnt++;
}
}
for(int i = 1; i <= line; i++) {
for(int j = 0; j < w[i].size(); j++) {
s1 = w[i][j];
cout << s1;
if(j!=w[i].size()-1) {
for(int k = s1.length(); k <= h[j+1]; k++) {
cout <<' ';
}
}
}
cout <<endl;
}
return 0;
}