UVA - 1593 Alignment of Code
题目大意:输入字符串, 要求输出每列对齐
解题思路:格式控制,要注意每行最后一个单词不用空格补齐 不然会wa!!!
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <algorithm>
#include <sstream>
using namespace std;
string code;
vector<string> str[2000];
int m[2000];
void pri(string s, int x) {
cout << s;
for(int i = 0; i < x - s.size(); i++) {
cout << " ";
}
cout << " ";
}
int main() {
int n = 0;
memset(m, 0, sizeof(m));
while(getline(cin, code)) {
stringstream ss(code);
string st;
int p = 0;
while(ss >> st) {
m[p] = max((int)st.size(), m[p]);
str[n].push_back(st);
p++;
}
n++;
}
for(int i = 0; i < n; i++) {
int j;
for(j = 0; j < str[i].size()-1; j++) {
pri(str[i][j], m[j]);
}
cout << str[i][j] << endl;
}
return 0;
}