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;
}

本文介绍了一道UVA-1593的编程题,要求输入的字符串进行格式化处理,使每列的文字都能整齐对齐。通过使用C++实现,文章详细解释了如何利用字符串流来解析输入,并采用最大宽度填充的方法实现输出。

633

被折叠的 条评论
为什么被折叠?



