Alignment of Code (UVA - 1593 )

版权声明:欢迎转载!拒绝抄袭. https://blog.csdn.net/qq_36257146/article/details/87358619

题意如下:

输入若干行代码,要求各列单词的左边界对齐且尽量靠左。

单词之间至少要空一格。每个单词不超过80个字符,

每行不超过180个字符,一共最多1000行。

#include <iostream>
#include <bits/stdc++.h>
#define maxn 1001
using namespace std;
vector<string>code[maxn];
int len[maxn];

int main()
{
    string s,t;
    int row = 0,col;
    memset(len,0,sizeof(len));
    while(getline(cin,s))
    {
        col = 0;
        stringstream ss(s);
        while(ss>>t)
        {
             code[row].push_back(t);
             len[col] = len[col]>t.length()?len[col]:t.length();
             col++;
        }
        row++;
    }
    //cout<<row;
//    for(int i = 0;i<row;i++)
//    {
//        for(int j = 0;j<code[i].size();j++)
//        {
//            cout<<code[i][j]<<" ";
//        }
//        cout<<endl;
//    }
    for(int i = 0;i<row;i++)
    {
        for(int j = 0;j<code[i].size()-1;j++)
        {
            cout<<code[i][j];
            int lenM = len[j]-code[i][j].size()+1;
            while(lenM--) cout<<' ';
        }
        cout<<code[i][code[i].size()-1];
        cout<<endl;
    }
    return 0;
}
/**
  start:  integer;    // begins here
stop: integer; //  ends here
 s:  string;
c:   char; // temp
**/

猜你喜欢

转载自blog.csdn.net/qq_36257146/article/details/87358619
今日推荐