Center Alignment

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=93359#problem/B(456321)

http://codeforces.com/problemset/problem/5/B

Center Alignment
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

#define N 1100

struct node
{
    char s[N];
    int len;
}a[N];

int main()
{
    int n=0, MaxLen=0, i, j;
    char s1[N];

    memset(a, 0, sizeof(a));

    while(gets(s1))
    {
        strcpy(a[n].s, s1);
        a[n].len = strlen(s1);
        if(a[n].len>MaxLen)
            MaxLen = a[n].len;
        n++;
    }

   /// printf("%d
", MaxLen);
    for(int i=0; i<MaxLen+2; i++)
        printf("*");

    printf("
");

    int flag = 1, ans;
    for(i=0; i<n; i++)
    {
        printf("*");
        int len = MaxLen-a[i].len;

        if(len%2)
            flag = !flag;

        ans = (flag+len)/2;
        for(j=1; j<=ans; j++)
            printf(" ");
        printf("%s", a[i].s);

        for(j=1; j<=len-ans; j++)
            printf(" ");

        printf("*
");
    }


    for(int i=0; i<MaxLen+2; i++)
        printf("*");

    printf("
");

    return 0;
}
View Code
勿忘初心
原文地址:https://www.cnblogs.com/YY56/p/4851930.html