题目描述:
解题思路:
输入时提出单个字符串,并用一个数组记录每列最长长度,格式化输出
1 #include <iostream> 2 #include <algorithm> 3 #include <string> 4 #include <sstream> 5 #include <vector> 6 using namespace std; 7 8 vector<string> words[1005] ; 9 int lmax[255] ; 10 int main(int argc, char *argv[]) 11 { 12 string buf,str ; 13 int k=0,i=0; 14 while(getline(cin,str)){ 15 stringstream ss(str) ; 16 while(ss >> buf){ 17 int len = buf.size() ; 18 lmax[k] = max(lmax[k],len) ; // 每列最长字符 19 words[i].push_back(buf) ; 20 k++ ; 21 } 22 i++ ; 23 } 24 for(int j = 0; j<i; j++) 25 { 26 for(int k = 0; k<words[j].size(); k++) 27 { 28 cout << words[j][k]; 29 if(k==words[i].size()-1) 30 break; 31 int siz = (int)words[j][k].size(); 32 for(int l = 0; l < lmax[k]-siz; l++) 33 cout << " "; 34 cout << " "; 35 } 36 cout << endl; 37 } 38 return 0; 39 }
原文地址:https://www.cnblogs.com/secoding/p/9495611.html
时间: 2024-11-12 15:27:43