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 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; }
/* 描述 给定任意一个N(>=5)个字符的字符串,您将被要求将字符形成U的形状。 例如,“helloworld”可以打印为:h d e l l r lowo 也就是说,字符必须按原始顺序打印,从左垂直行自上而下开始,共有n1个字符, 然后沿底行从左向右打印,共有n2个字符,最后沿着具有n3个字符的垂直线自下而上。 而且,我们希望U尽可能方——也就是说, 它必须满足n1=n3=max{k|k<=n2,对于n1+n2+n3-2=N的所有3<=n2<=N}。 输入描述: 有多个测试用例。每个用例包含一个字符串, 一行中不少于5个字符,不超过80个字符。该字符串不包含空白。 输出描述: 对于每个测试用例,将输入字符串打印为描述中指定的U形。 */ #include <array> #include <iostream> #include <string> using namespace std; int main() { string str; while (cin >> str) { int n = str.size(); int n1, n2, n3; n1 = n3 = (n + 2) / 3; n2 = n + 2 - n1 - n3; array<array<char, 80>, 80>matrix; for (int i = 0; i < n1; i++) { for (int j = 0; j < n2; j++) { matrix[i][j] = ' '; } } for (int i = 0; i < n1; i++) { matrix[i][0] = str[i]; matrix[i][n2 - 1] = str[n - 1 - i]; } for (int i = n1, count = 1; count <= n2 - 2; i++, count++) { matrix[n1 - 1][count] = str[i]; } for (int i = 0; i < n1; i++) { for (int j = 0; j < n2; j++) { cout << matrix[i][j]; } cout << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string str; while (cin >> str) { int N = str.length(); int n1 = (N+2)/3, n2 = N-n1*2+2; int left = 0, right = N - 1; for (int i = 0; i < n1; i++) { for (int j = 0; j < n2; j++) { if (j == 0 || i == n1-1) {a cout << str.substr(left, 1); left++; } else if (j == n2 - 1 && right > left) { cout << str.substr(right, 1); right--; } else { cout << ' '; } } cout << endl; } } return 0; }
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { int n, h, w; char s[100]; while (gets(s)) { n = strlen(s); h = n/3; w = n-2*h; if (w<=h) { h --; w += 2; } for (int i = 0; i < h; i ++) { printf("%c", s[i]); for(int j = 0; j < w-2;j++){ printf(" "); } printf("%c\n", s[n-i-1]); } for (int k = 0; k < w; k ++) { printf("%c", s[h+k]); } printf("\n"); } return 0; }
#include <iostream> using namespace std; int main() { string s; while (cin >> s) { int len = s.size(); int row = len / 3; if(len%3!=0){ row++; } for (int i = 0; i < row; ++i) { for (int j = 0; j < len-2*(row-1); ++j) { if (j == 0) { cout << s[i + j]; } else if (j == len-2*(row-1)-1) { cout << s[len - i -1]; } else if (i < row - 1) { cout << " "; } else { cout << s[i + j]; } } cout << endl; } } } // 64 位输出请用 printf("%lld")
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { int n, h, w; char s[100]; fgets(s, 100, stdin); n = strlen(s)-1; h = n/3; w = n-2*h; if (w<=h) { h --; w += 2; } for (int i = 0; i < h; i ++) { printf("%c", s[i]); for(int j = 0; j < w-2;j ++){ printf(" "); } printf("%c\n", s[n-i-1]); } for (int k = 0; k < w; k ++) { printf("%c", s[h+k]); } return 0; }