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;
}
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;
}
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- android上改变listView的选中颜色
- String.intern
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- PostgreSQL ERROR: invalid escape string 解决办法
- 浅谈C++中的string 类型占几个字节
- 标准C++类string的Copy-On-Write技术
- C++实现string存取二进制数据的方法
- C#中string和StingBuilder内存中的区别实例分析
- PHP STRING 陷阱原理说明
- c#中 String和string的区别介绍
- C#实现的图片、string相互转换类分享
- asp.net String.IsNullOrEmpty 方法
- JavaScript中字符串(string)转json的2种方法
- C#中string用法实例详解
- C#中的DataSet、string、DataTable、对象转换成Json的实现代码
- JavaScript中的object转换成number或string规则介绍