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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值