1031 Hello World for U (20 分)
思路
获得满足要求的n1,n2,这种题需要多输入几个不同长度的串去检验
修改string字符串比较困难,需要使用string类函数,所以使用char字符数组
还有一个细节,char字符数组PAT后置不默认为’\0’,要自己封
代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
cin >> str;
int len = str.length();
int n1 ,n2;
for (int i = 3;i<=len;i++) //获得满足要求的n1,n2
{
n2 = i;
n1 = (len + 2 -n2)/2;
if(n1<=n2)
{
n2 = len+ 2 - n1*2;
break;
}
}
char out[100] ;
for(int i=0;i<n1;i++)
{
if(i!=n1-1)
{
out[0]=str[i];
for(int j =1 ;j< n2-1 ;j++)
{
out[j] = ' ';
}
out[n2-1]=str[len-1-i];
out[n2]='\0'; //PAT后置不默认为'\0',要自己封
cout<<out<<endl;
}
else
{
for(int j =n1-1 ;j< n1+n2-1 ;j++)
{
out[j-n1+1] = str[j];
}
out[n2]='\0';
cout<<out;
}
}
}