A1031. Hello World for U
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 to be as squared as possible -- that is, it must be satisfied that n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.
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
1 #include<cstdio> 2 #include<iostream> 3 #include<string.h> 4 using namespace std; 5 int main(){ 6 char str[81], print[30][30]; 7 int len, n1, n2, n3; 8 for(int i = 0; i < 30; i++) 9 for(int j = 0; j < 30; j++) 10 print[i][j] = ' '; 11 scanf("%s", str); 12 len = strlen(str); 13 n1 = n3 = (len + 2) / 3; 14 n2 = len + 2 - n1 - n3; 15 int i; 16 for(i = 0; i < n1; i++){ 17 print[i][0] = str[i]; 18 } 19 for(int j = 1; j < n2; j++, i++){ 20 print[n1 - 1][j] = str[i]; 21 } 22 for(int k = n1 - 2; k >= 0; k--, i++){ 23 print[k][n2 - 1] = str[i]; 24 } 25 for(int j = 0; j < n1; j++){ 26 for(int k = 0; k < n2; k++) 27 printf("%c", print[j][k]); 28 printf("\n"); 29 } 30 cin >> i; 31 return 0; 32 33 }
总结:
1、打印输出题两种方法,先将其处理在二维字符数组中再逐行输出。或者直接按规律打印输出。根据题意找规律很重要。
2、涉及到字符数组处理时可以使用string.h