CF 5 B - Center Alignment

Submit Status Practice CodeForces 5B

Description

Almost every text editor has a built-in function of center text alignment. The developers of the popular in Berland text editor «Textpad» decided to introduce this functionality into the fourth release of the product.

You are to implement the alignment in the shortest possible time. Good luck!

Input

The input file consists of one or more lines, each of the lines contains Latin letters, digits and/or spaces. The lines cannot start or end with a space. It is guaranteed that at least one of the lines has positive length. The length of each line and the total amount of the lines do not exceed 1000.

Output

Format the given text, aligning it center. Frame the whole text with characters «*» of the minimum size. If a line cannot be aligned perfectly (for example, the line has even length, while the width of the block is uneven), you should place such lines rounding down the distance to the left or to the right edge and bringing them closer left or right alternatively (you should start with bringing left). Study the sample tests carefully to understand the output format better.

Sample Input

Input
This  is

Codeforces
Beta
Round
5
Output
************
* This  is *
*          *
*Codeforces*
*   Beta   *
*  Round   *
*     5    *
************
Input
welcome to the
Codeforces
Beta
Round 5

and
good luck
Output
****************
*welcome to the*
*  Codeforces  *
*     Beta     *
*   Round 5    *
*              *
*      and     *
*  good luck   *
****************
题意:文章排版问题,让文字居中显示。如果两边不对称时,一个靠左显示,下一个不对称的靠右显示。
题意半天没读懂,还是在别人说了的情况下改对的,…………快哭了
 
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char str[1005][1005];
int main()
{
    int mmax=-1,t=0,k;
    while(gets(str[t]))
    {
        //if(strcmp(str[t],"4")==0)break;
        int len=strlen(str[t]);
        if(mmax<len)
        {
            mmax=len;
            k=t;
        }
        t++;
    }
    int num=0;
    for(int i=0; i<mmax+2; i++)
        cout<<"*";
    cout<<endl;
    for(int i=0; i<t; i++)
    {
        int l=strlen(str[i]);
        if(l==mmax)
            cout<<"*"<<str[i]<<"*"<<endl;
        else
        {

            cout<<"*";
            int ll=mmax-l;
            int s=ll/2;
            if(ll%2!=0)
            {
                num++;
                if(num%2!=0)
                {
                    for(int j=0; j<s; j++)
                        cout<<" ";
                    cout<<str[i];
                    for(int j=0; j<ll-s; j++)
                        cout<<" ";
                    cout<<"*"<<endl;
                }
                else
                {
                    for(int j=0; j<ll-s; j++)
                        cout<<" ";
                    cout<<str[i];
                    for(int j=0; j<s; j++)
                        cout<<" ";
                    cout<<"*"<<endl;
                }
            }
            else
            {
                for(int j=0; j<s; j++)
                    cout<<" ";
                cout<<str[i];
                for(int j=0; j<ll-s; j++)
                    cout<<" ";
                cout<<"*"<<endl;
            }
        }
    }
    for(int i=0; i<mmax+2; i++)
        cout<<"*";
    cout<<endl;
    return 0;
}
/*
This  is

Codeforces
Beta
Round
5

welcome to the
Codeforces
Beta
Round 5

and
good luck
*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值