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!
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.
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.
This is Codeforces Beta Round 5
************ * This is * * * *Codeforces* * Beta * * Round * * 5 * ************
welcome to the Codeforces Beta Round 5 and good luck
**************** *welcome to the* * Codeforces * * Beta * * Round 5 * * * * and * * good luck * ****************
题意:
给出一些字符串,让你用 * 号组成的最小矩形把这些字符串围起来,不足的补充空格,所有字符串中间对齐,如果出现长度的奇偶性不同,那就优先空着左边,下次再优先空着右边,输出最终处理的结果
题解:
字符串的处理,使用string 类型比较方便,这道题总体比较简单,但是有一个坑点:空行(长度为0)和所有的其他行是对齐的,处理的时候要特殊注意
错了几遍才想到这种特殊情况,自己的思维还不够细致
/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
string s[10005];
int main()
{
//freopen("shuju.txt","r",stdin);
int cnt=0,len=0,sign=0;
while(getline(cin,s[cnt]))
{
int tp=s[cnt++].length();
len=max(len,tp);
}
for(int i=0;i<len+2;++i)
{
cout <<"*";
}
cout <<"\n";
for(int i=0;i<cnt;++i)
{
int tplen=s[i].length();
if((tplen&1)!=(len&1)&&tplen)//无法完全对齐
{
++sign;
}
int tp=len-tplen;
int ta=tp-tp/2,tb=tp-ta;//a
if(sign&1)
{
swap(ta,tb);
}
cout <<"*";
for(int j=0;j<ta;++j)
{
cout <<" ";
}
cout <<s[i];
for(int j=0;j<tb;++j)
{
cout <<" ";
}
cout <<"*\n";
}
for(int i=0;i<len+2;++i)
{
cout <<"*";
}
cout <<"\n";
return 0;
}