http://pat.zju.edu.cn/contests/pat-practise/1031

1031. Hello World for U (20)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 n 1characters, then left to right along the bottom line with n 2 characters, and finally bottom-up along the vertical line with n 3 characters. And more, we would like U to be as squared as possible -- that is, it must be satisfied that n 1 = n 3 = max { k| k <= n 2 for all 3 <= n 2 <= N } with n 1 + n 2 + n 3 - 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

[cpp]  view plain copy
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9. using namespace std;  
  10. #define MAX 0Xfffffff  
  11. char s[100];  
  12. int main(){  
  13.   
  14.    //freopen("in.txt", "r", stdin);  
  15.     
  16.    cin>>s;  
  17.    int len = strlen(s);  
  18.    int n2;  
  19.    int n1 = 0;  
  20.    for(int x=3;x<=len;++x){  
  21.      int k = (len+2-x)/2;  
  22.      if(k>x)  
  23.          continue;  
  24.      if(k>n1){  
  25.          n2 = x;  
  26.          n1 = k;  
  27.      }  
  28.    }  
  29.   
  30.     
  31.    for(int i=0;i
  32.         printf("%c", s[i]);  
  33.           
  34.         if(i!=n1-1)  
  35.             for(int j=0;j
  36.                 printf(" ");  
  37.         else{  
  38.             for(int j=n1;j
  39.                 printf("%c", s[j]);  
  40.         }  
  41.           
  42.         printf("%c", s[len-1-i]);  
  43.         printf("\n");  
  44.    }  
  45.      
  46.    printf("\n");  
  47.   
  48.    //fclose(stdin);  
  49. }     

你可能感兴趣的:(PAT)