PAT 1031(patriot)

网友投稿 234 2022-09-20


PAT 1031(patriot)

0.题目 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 n 1 characters, 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 1.题目分析 (1)本题的意图是让我们根据一个字符串的原有顺序,打印出一个接近正方形的U字形。 (2)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. 根据下面的示意图,我们来分析一下这句话, h ! e d l l lowor 这里n1就是从字母‘h’到第二次出现字母'l',n2所代表的长度就是自底部最左边到底部最右边的字符长度,即'l'到‘r’的长度,同理分析可得n3为'!'到'r'的长度。 (3)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. 这句话的意思就是,n1= n3=max{k|k<=n2,for all 3<=n2<=N},即对于所有的n1,n3取最大的k值,这里的k值小于等于n2,并且满足3<=n2<=N. (4)综上所述,我们可以得到以下数学分析式: n1 = n3 <(N+2)/3;这里的N为字符串的长度!注意了,这里我们没有用n2来分析表达式【如果用n2分析表达式的话则为:n2 >(N+2)/3】,不使用n2作为根据的原因是:这个表达式不能精确的得到n2值。比如说,对于字符串“Hello”,如果用n2>(N+2)/3,那么我们可以得到满足条件的最小n2 =3,此处满足条件;但是当字符串为“HelloU”时,就会发现满足条件的最小n2= 3,但是实际上为n2 = 4。 2.代码 #include #include int main() { char string[100];// N; scanf("%s",string); int length = strlen(string); int n1,n2,n3;// //因为n1与n3只会往小取 ---> 所以先取n1和n3的值,因为除法所得的商是较小值 ,再根据n1和n3取n2的值 n1 = n3 = (length+2)/3; n2 = length +2 - (2 * n1); int i =0,j = 0; while(i

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springboot restTemplate连接池整合方式
下一篇:散列表在PAT中的应用,例题:1041,1050,1084
相关文章

 发表评论

暂时没有评论,来抢沙发吧~