CF_5B_CenterAlignment

来源:互联网 发布:c语言 字符串统计函数 编辑:程序博客网 时间:2024/05/29 16:24

B. Center Alignment
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

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.

Examples
input
This  isCodeforcesBetaRound5
output
************* This  is **          **Codeforces**   Beta   **  Round   **     5    *************
input
welcome to theCodeforcesBetaRound 5andgood luck
output
*****************welcome to the**  Codeforces  **     Beta     **   Round 5    **              **      and     **  good luck   *****************

题意给给定的字符块

加一个框子,并给每行居中

不能完全居中的先左一格后右一格 交替


#include <iostream>#include <stdio.h>#include <string.h>using namespace std;const int M=1005;char s[M][M];int sl[M];int main(){    int num=0;    int msl=0;//最大串长度    int lr=1; //记录下次向左向右    //freopen("1.in","r",stdin);    while(gets(s[num])!=NULL)    {        sl[num]=strlen(s[num]);        msl=max(msl,sl[num]);        num++;    }    for(int i=1;i<=msl+2;i++)  //上框        printf("*");    printf("\n");    for(int i=0;i<num;i++)    {        printf("*");        if((msl-sl[i])&1)        {            if(lr)            {                for(int j=0;j<(msl-sl[i])/2;j++)                    printf(" ");                printf("%s",s[i]);                for(int j=0;j<=(msl-sl[i])/2;j++)                    printf(" ");            }            else            {                for(int j=0;j<=(msl-sl[i])/2;j++)                    printf(" ");                printf("%s",s[i]);                for(int j=0;j<(msl-sl[i])/2;j++)                    printf(" ");            }            lr=1-lr;        }        else        {            for(int j=0;j<(msl-sl[i])/2;j++)                printf(" ");            printf("%s",s[i]);            for(int j=0;j<(msl-sl[i])/2;j++)                printf(" ");        }        printf("*\n");    }    for(int i=1;i<=msl+2;i++)  //下框        printf("*");    printf("\n");    return 0;}


0 0
原创粉丝点击