PAT 1031 Hello World for U
阿新 • • 發佈:2018-12-05
1031 Hello World for U (20 分)
Given any string of N (≥5) characters, you are asked to form the characters into the shape of U
. For example, helloworld
can be printed as:
h d
e l
l r
lowo
That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1 characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters. And more, we would like U
Input Specification:
Each input file contains one test case. 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.
Output Specification:
For each test case, print the input string in the shape of U as specified in the description.
Sample Input:
helloworld!
Sample Output:
h !
e d
l l
lowor
程式碼
//題目要求將一行字串變成U型,n1為左邊一列,n2為最下面一行,n3為右邊一列 #include<iostream> #include<cstdio> #include<cstring> using namespace std; int Find(char *str,int l) { int maxl=0; //i表示n2 for(int i=3; i<=l; i++) { int x=l-i; //x為2*(n1-1) //n1<=n2,那麼n1-1<n2 if(x%2==0&&x/2>=maxl&&x/2<i) maxl=x/2; } return maxl; } int main() { char str[100]; while(~scanf("%s",str)) { int l=strlen(str); int n=Find(str,l); //printf("%d\n",n); int c=0; while(c<n) { printf("%c",str[c]); for(int i=0; i<l-2*n-2; i++) printf(" "); printf("%c\n",str[l-1-c]); c+=1; } for(int i=c; i<l-c; i++) printf("%c",str[i]); } return 0; }