您的位置:首页 > 其它

CodeForces 5B - Center Alignment

2015-08-13 17:30 351 查看
1、主要就是读懂题意,然后就是处理字符串了

2、判断每句有多少个字符,然后根据剩下的字符,再做判断

3、最长的字符数去减去每句的字符数,剩下的字符数分成左右两部分,一次左边多,一次右边多,这样就好了#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <stack>
#include <queue>
#include <string>
#include <cstring>
#include <iomanip>
#include <set>
#include <vector>
#include <cstdio>
#include <stack>
#include <sstream>
#include <cstring>
#include <map>
using namespace std;
#define TEST
int main()
{
//#ifdef TEST
// freopen("C:\\Users\\MrZis\\Desktop\\input.txt","r",stdin);
// freopen("C:\\Users\\MrZis\\Desktop\\output.txt","w",stdout);
//#endif
string str;
string str1[1005];
int max_lenth=0,n=0;
while(getline(cin,str))
{
str1
=str;
n++;
int tmp=str.size();
max_lenth=max(max_lenth,tmp);
}
for(int i=1;i<=max_lenth+2;i++)
cout<<"*";
cout<<endl;
bool flag=0;
//cout<<max_lenth;
//cout<<n<<endl;
for(int i=0;i<n;i++)
{
cout<<"*";
int lenth=str1[i].size();
//cout<<lenth<<endl;
int rest_lenth=max_lenth-lenth;
//cout<<rest_lenth<<endl;
// cout<<str1[i]<<endl;
if(rest_lenth%2==0)
{
for(int j=1;j<=rest_lenth/2;j++)
cout<<" ";
cout<<str1[i];
for(int j=(rest_lenth/2+lenth+1);j<=max_lenth;j++)
cout<<" ";
}
else if(rest_lenth%2==1)
{
if(flag==0)
{
for(int j=1;j<=rest_lenth/2;j++)
cout<<" ";
cout<<str1[i];
for(int j=(rest_lenth/2+lenth+1);j<=max_lenth;j++)
cout<<" ";
flag=1;
}
else if(flag==1)
{
for(int j=1;j<=rest_lenth/2+1;j++)
cout<<" ";
cout<<str1[i];
for(int j=(rest_lenth/2+lenth+2);j<=max_lenth;j++)
cout<<" ";
flag=0;
}
}
cout<<"*"<<endl;
}
for(int i=1;i<=max_lenth+2;i++)
cout<<"*";
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string CF