码迷,mamicode.com
首页 > 其他好文 > 详细

PAT1031. Hello World for U

时间:2015-02-04 12:25:02      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

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

思路:本程序遇到的一个问题就是如何去计算n1,n2,n3的值,在之前的公式中没有发现其中的含义,公式的意思是n1,n3取最大值的时候n2>n3,并且3<=n2<=N,并且n1+n2+n3=3
将其存储到数组中去,然后讲数组打印出来。另外遇到的一个问题是用法fill fill在初始化二维数组的时候 需要G[0]
技术分享
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 char str[90];
 6 char Print[90][90];
 7 int main(int argc, char *argv[])
 8 {
 9     fill(Print[0],Print[0]+90*90, ); //二维数组赋初值需要注意  fill需要带0 
10     scanf("%s",str);
11     int length=strlen(str);
12     int n1,n2,n3;
13     for(n2=3;n2<=length;n2++)
14     {
15         int temp=length+2-n2;
16         if(temp%2!=0)
17            continue;
18         else
19          {
20               if(temp/2<=n2)
21             {
22                 n1=n3=temp/2;
23                 break;
24             }
25             else
26                continue;     
27            } 
28     }
29     int count=0;    
30     for(int i=1;i<=n1;i++)
31     {
32         Print[i][1]=str[count++];
33     }
34     for(int j=2;j<=n2;j++)
35     {
36         Print[n1][j]=str[count++];
37     }
38     for(int i=n1-1;i>=1;i--)
39     {
40         Print[i][n2]=str[count++];
41     }
42     for(int i=1;i<=n1;i++)
43     {
44         for(int j=1;j<=n2;j++)
45            printf("%c",Print[i][j]);
46         printf("\n");
47     }
48     return 0;
49 }
View Code

 

PAT1031. Hello World for U

标签:

原文地址:http://www.cnblogs.com/GoFly/p/4271842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!