uva 1593

来源:互联网 发布:云南大学软件学院 编辑:程序博客网 时间:2024/06/05 14:45

一道字符串处理的题目,用len数组记录每一列单词所占的格子数。然后使用一个vector数组保存所有的单词,依照每一列所占的大小输出。

#include <iostream>#include <sstream>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;vector<string> s[1005];int len[185];void print(string cm,int len){    cout<<cm;    int cnt = len - cm.length();    //cout<<"  "<<cnt;    while(cnt--) cout<<" ";}int main(){    string str,buf;    int i = 0,j = 0;    while(getline(cin,str)){        istringstream stream(str);        j=0;        while(stream >> buf){            len[j] = max(len[j],(int)buf.length());            j++;            s[i].push_back(buf);        }        i++;j=0;    }    for(int k = 0;k<i;k++){        for(int l = 0;l<s[k].size()-1;l++){            print(s[k][l],len[l]+1);        }        cout<<s[k][s[k].size()-1]<<endl;    }    return 0;}
0 0