看上去十分麻烦的一道题,但是看了看别人的写法感觉大神们写的无比简单。
就是记一个每列单词的最大长度,然后剩下的事交给NB的iomanip头文件就好。
stringsteam是一个神奇的东西。
#include <iostream> #include <cstdio> #include <iomanip> #include <sstream> #include <string> #include <cstring> #include <vector> using namespace std; #define maxn 1000 + 100 vector<string> code[maxn]; int maxlen[maxn]; int main() { string s; int cas = 0; while (getline(cin, s)) { stringstream ss(s);//用这个字符串流可以从字符串中输入文件 string word; int tot = 0; while(ss >> word) { maxlen[tot] = max(maxlen[tot], (int)word.size()); code[cas].push_back(word); ++tot; } ++cas; } cout << setiosflags(ios :: left); for (int j = 0; j < cas; j++) { int i = 0; for (i; i < code[j].size()-1; i++) cout << setw(maxlen[i] + 1) << code[j][i]; cout << code[j][i] << endl; } }