其他分享
首页 > 其他分享> > J - Alignment of Code(25) (UVA - 1593 )

J - Alignment of Code(25) (UVA - 1593 )

作者:互联网

此题为字符串对齐

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int MAX_len[200], MAX_num;
struct line
{
    char str[200];
    int num[200];
    int len;
} L[1010];

char str[200];
int main()
{
    int cnt = 0;
    memset(MAX_len,0,sizeof(MAX_len));
    while(gets(str))
    {
        int num = 0, count = 0, len = strlen(str);
        bool flag = true;
        for(int i = 0; i < len ; i++)
        {
            if(flag && str[i] == ' ')
            {
                continue;
            }
            else if(flag && str[i] != ' ')
            {
                flag = false;
                num++;
                L[cnt].str[count++] = ' ';
                L[cnt].str[count++] = str[i];
            }
            else if(!flag && str[i] != ' ')
            {
                L[cnt].str[count++] = str[i];
            }
            else if(!flag && str[i] == ' ')
            {
                flag = true;
            }
        }
        L[cnt].str[count] = '\0';
        L[cnt].len = count;
        count = 0, num = 0;
        for(int j = 1; j <= L[cnt].len; j++)
        {
            if(L[cnt].str[j] == ' ' || j == L[cnt].len)
            {
                MAX_len[num] = max(MAX_len[num],count);
                count = 0;
                num++;
            }
            else
            {
                count++;
            }
        }
        MAX_num = max(MAX_num,num);
        cnt++;
    }
    for(int i = 0; i < cnt; i++)
    {
        int cnt = 0, Num = 0, sum = 0;
        for(int j = 1; j < L[i].len; j++)
        {
            if(L[i].str[j] != ' ')
            {
                printf("%c",L[i].str[j]);
                cnt++;
                sum++;
            }
            else
            {
                for(int k = cnt; k < MAX_len[Num]; k++)
                {
                    printf(" ");
                    sum++;
                }
                Num++;
                cnt = 0;
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
}
#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;
}

 

标签:25,Code,1593,int,len,++,flag,str,include
来源: https://blog.csdn.net/qq_44134712/article/details/94043113