5BCenter Alignment

来源:互联网 发布:Js undefunde 与 null 编辑:程序博客网 时间:2024/05/29 03:53

这题不难,但是很考细节,写着就迷糊了。调试一大堆问题~还有就是这是有空格的字符串,所以需要用gets能一行读入字符串包括空格,getline也行,%s,cin,都不能读入空格~刚开始做没什么思路,在怎么想才能对齐,就是想不到,不得参考博主们的代码和思路,看懂了就自己写~菜~都大二呢,慢慢来,一点一点积累,cf上的题真的很锻炼思想~大家共勉~
B. Center Alignment
time limit per test1 second
memory limit per test64 megabytes
inputstandard input
outputstandard 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 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<cstdio>                #include<cstring>                #include<string>                #include<algorithm>                using namespace std;                const int N=1e3+10;                int main()                {                char s[N][N];                int a[N];                int maxl=0,l,lr,cnt=0;                lr=1;                while(gets(s[cnt]))                {                    a[cnt]=strlen(s[cnt]);                    maxl=max(a[cnt],maxl);                    cnt++;                }                for(int i=0; i<maxl+2; i++)                    printf("*");                printf("\n");                for(int i=0; i<cnt; i++)                {                    printf("*");                    int n=(maxl-a[i]);                    if(n%2)                    {                        if(lr)                        {                            for(int j=0; j<n/2; j++)                                printf(" ");                            printf("%s",s[i]);                            for(int j=0; j<=n/2; j++)                                printf(" ");                        }                        else                        {                            for(int j=0; j<=n/2; j++)                                printf(" ");                            printf("%s",s[i]);                            for(int j=0; j<n/2; j++)                                printf(" ");                        }                        lr=1-lr;                    }                    else                    {                        for(int j=0; j<n/2; j++)                            printf(" ");                        printf("%s",s[i]);                        for(int j=0; j<n/2; j++)                            printf(" ");                    }                    printf("*\n");                }                for(int i=0; i<maxl+2; i++)                    printf("*");                return 0;            }