There are multiple test cases.Each case contains one string with no less than 5 and no more than 80 characters in a line. The string contains no white space.
For each test case, print the input string in the shape of U as specified in the description.
helloworld! www.nowcoder.com
h ! e d l l lowor w m w o w c . . n r owcode
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
while (cin>>str) {
int n = str.length();
int n1 = (n + 2) / 3;
int n2 = n - n1*2 + 2;
for (int i = 0; i < n1 - 1; i++) {
cout<<str[i];
for (int i = 0; i < n2 - 2; i++) {
cout<<' ';
}
cout<<str[n-1-i]<<endl;
}
for (int i = -1; i < n2 - 1; i++) {
cout<<str[n1+i];
}
cout<<endl;
}
return 0;
} #include<bits/stdc++.h>
using namespace std;
int main(){
string str;
while(getline(cin,str)){
int n=str.length();
int x,y;
if((n+2)%3==0){
x=y=(n+2)/3;
}else if((n+4)%3==0){
y=(n+4)/3;
x=y-1;
}else if((n+6)%3==0){
y=(n+6)/3;
x=y-2;
}
for(int i=0;i<x-1;i++){
cout<<str[i];
for(int j=0;j<y-2;j++)cout<<" ";
cout<<str[n-i-1]<<endl;
//cout<<endl;
}
for(int i=x-1;i<=n-x;i++)cout<<str[i];
cout<<endl;
}
return 0;
} #include<iostream>
(720)#include<string>
using namespace std;
int main(){
string s;
while(cin>>s){
int len=s.size();
len+=2;
int l=len/3-1;//(len+2)/3就是n1和n2的值
int mid=len-len/3*2;
for(int i=0;i<l;i++){
cout<<s[i];
for(int j=0;j<mid-2;j++)cout<<" ";
cout<<s[s.size()-1-i]<<endl;
}for(int i=l;i<l+mid;i++){
cout<<s[i];
}
}
return 0;
} #include<iostream>
(720)#include<string>
using namespace std;
int main(){
string str;
while(cin>>str){
int N=str.length();
int n2 = (N+2)%3==0 ? (N+2)/3 : (N+2)/3+1;
if( (n2%2==0 && N%2==1) || (n2%2==1 && N%2==0)) n2++;
int n1 = (N-n2)/2;//不包括与n2重叠的
int n3 = (N-n2)/2;//不包括与n2重叠的
//共需要n1+1行,n2列
int blank_number = n2-2;
int index_first=0;
int index_last=N-1;
for(int i=0;i<n1;i++){
int temp_blank=blank_number;
cout<<str[index_first++];
while(temp_blank-- > 0) cout<<" ";
cout<<str[index_last--]<<endl;
}
for(int i=0;i<n2;i++){
cout<<str[index_first++];
}
}
return 0;
} #include<cstdio>
#include<string.h>
using namespace std;
int main(){
char str[80];
while(~scanf("%s",str)){
int i, n1=0;
int N=strlen(str);
for(i=1;i<N;i++){ /*先计算n1和n2*/
if(N+2-2*i>=i && i>n1)
n1=i;
}
int n2=N+2-2*n1;
int j=N, k; /*按行输出*/
for(i=0,j=N-1;j-i+1>n2;i++,j--){
printf("%c",str[i]); /*输出每行第一个字符*/
for(k=0;k<n2-2;k++) /*输出中间的空格*/
printf(" ");
printf("%c\n",str[j]); /*输出每行最后一个字符*/
}
for(;i<=j;i++) /*输出最后一行*/
printf("%c",str[i]);
printf("\n");
}
return 0;
} #include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
while(cin>>s)
{
int n=s.length(),n1=(n+2)/3,n2=n+2-2*n1;
for(int i=0;i<n1-1;++i)
{
cout<<s[i];
for(int j=0;j<n2-2;++j)
cout<<" ";
cout<<s[n-i-1]<<endl;
}
cout<<s.substr(n1-1,n2)<<endl;
}
return 0;
}
#Python实现,将字符串平均分为三份,如果有余数的都给横排。
#如果余数为零就要分3个给最下方,因为竖排不能比横排多,余数为零表示竖排比横排多一
try:
while True:
string = list(input())
a,b = divmod(len(string),3) #得到除数和余数
if b<1: #如果余数为0
a-=1
b+=3
temp = " "*(a+b-2) #得到中间的空格数
for i in range(a):
print(string.pop(0)+temp+string.pop(-1)) #输出头和尾
print("".join(string)) #最后一行剩余的当做一个字符串输出
except Exception:
pass #include <iostream>
using namespace std;
int main() {
string a;
cin>>a;
int N=a.length();
int n1,n2;
for(int j=(N+2)/2;j>=0;j--){//解n1值
n2=N+2-2*j;
if(n2>=j) {
n1=j;
break;
}//解n2值且需满足条件
}
char martix[100][100];
for(int cow=0;cow<n1;cow++){
martix[cow][0]=a[cow];
}//第一列
for(int column=1;column<n2-1;column++){
for(int row=0;row<n1;row++){
if(row!=n1-1){
martix[row][column]=' ';
}
else{
martix[row][column]=a[n1+column-1];
}
}
}
int count=0;
for(int row=n1-1;row>=0;row--){
martix[row][n2-1]=a[n1-1+n2-1+count];
count++;
}//最后一列
// cout<<martix[0][0]<<"\n";
for(int row=0;row<n1;row++){
for(int column=0;column<n2;column++){
cout<<martix[row][column];
}
cout<<"\n";
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string a;
while(cin>>a)
{
int N = a.length();
int n1,n2,n3;
for(int i = (N+2)/2;i>=0;i--)
{
n2 = N+2-2*i;
if(n2>=i)
{
n1=i;
n3=i;
break;
}
}
char arr[100][100]={' '};
for(int be=0;be<n1;be++)
{
arr[be][0]=a[be];
}
int s = n1;
for(int be2=1;be2<n2;be2++)
{
arr[n1-1][be2]=a[s];
s++;
}
for(int be3=n3-1;be3>0;be3--)
{
arr[be3-1][n2-1] = a[s];
s++;
}
for(int h=0;h<n1;h++)
{
for(int l=0;l<n2;l++)
{
cout<<arr[h][l];
}
cout<<endl;
}
}
return 0;
} def U(s, n):
n1 = n2 = n // 3
r = n % 3
if r == 0:
n1 -= 1
n2 -= 1
n3 = n - 2 * n1
m1 = s[:n1]
m3 = s[n1:n3+n1]
m2 = s[n3+n1:n]
c = 0
i = 1
for _ in range(n1):
print(m1[c], end='')
for _ in range(n3-2):
print(' ', end='')
print(m2[-i])
i += 1
c += 1
print(m3)
while True:
try:
s = input()
n = len(s)
U(s, n)
except:
break #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
while (cin >> s) {
int len = s.length();
len += 2;
int n1 = len / 3;
int n2 = len - n1 * 2;
n2 -= 2;
for (int i = 0; i < n1; i++) {
cout << s[i];
if (i != n1 - 1) {
for (int j = 0; j < n2; j++) {
cout << " ";
}
} else {
for (int j = 0; j < n2; j++)
cout << s[n1 + j];
}
cout << s[len - 3 - i] << endl;
}
}
}
#include <iostream>
using namespace std;
int main() {
string temp;
int len, width,bottom;
while (true) {
getline(cin, temp);
len = temp.size();
// cout << "the string size:" << len << endl;
if (!len)return 0;
if (len % 3 == 2)
width = (len + 1) / 3;
else if(len%3 ==1)width = (len + 2) / 3;
else width=len/3;
bottom=len-2*width+2;
// cout << "the width:" << width << endl;
for (int i = 0; i < width; i++) {
if (i != width - 1)//不是最后一行
for (int j = 0; j < bottom; j++) {
if (j == 0)cout << temp[i];
else if (j == bottom-1 )cout << temp[len - i-1];
else cout << ' ';
}
else {
for (int k = 0;i + k < len - width+1; k++) {
cout << temp[i + k];
}
}
cout << endl;
}
}
}
// 64 位输出请用 printf("%lld") #include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
string str;
while(cin >> str){
int len=(str.size()+2);
vector<char> vec(len/3+1);
int idx=0;
int cnt;
if(len%3==0) cnt=len/3-1;
else if(len%3==1) cnt=len/3;
else cnt=len/3+1;
for(int i=0; i<len/3-1; ++i) {
cout << str[idx];
for (int j = 1; j < cnt; ++j) {
cout << ' ';
}
cout << str[str.size() - 1 - idx] << endl;
idx++;
}
for(int i=idx; i<str.size()-idx; ++i){
cout << str[i];
}
cout << endl;
}
}
#include <cstdio> using namespace std; int main() { char str[81];//最长字符长80 int N, n1, n2, n3; while (scanf("%s", str) != EOF) { int k = 0; while (str[k] != 0) ++k; N = k; //给定N,求n1和n2(n3=n1),2n1+n2=N+2,题意为使n1和n2尽量接近,但n2大于等于n1 n1 = (N + 2) / 3; n2 = N + 2 - 2 * n1; n3 = n1; for (int i = 0; i < n1 - 1; ++i) { for (int j = 0; j < n2; ++j) { if (j == 0) printf("%c", str[i]); else if (j == (n2 - 1)) printf("%c", str[N - 1 - i]); else printf(" "); } printf("\n");//换行 } //输出最后一行 for (int j = 0; j < n2; ++j) printf("%c", str[n1 - 1 + j]); //输出下一个需要换行 printf("\n"); } return 0; }