zoukankan      html  css  js  c++  java
  • PAT甲1031 Hello World for U【字符串】

    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 to be as squared as possible -- that is, it must be satisfied that n1​​=n3​​=max { k | kn2​​ for all 3n2​​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

    题意:

    将给定的字符串照U型输出。

    思路:

    找到小于len+2的最大的三的倍数,(len+2)/3就是竖着的个数。

     1 #include <iostream>
     2 #include <set>
     3 #include <cmath>
     4 #include <stdio.h>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <queue>
     9 #include <map>
    10 using namespace std;
    11 typedef long long LL;
    12 #define inf 0x7f7f7f7f
    13 
    14 char s[90];
    15 
    16 int main()
    17 {
    18     scanf("%s", s);
    19     int n = strlen(s);
    20     int x = (n + 2) / 3;
    21     int y = n - 2 * x;
    22 
    23     for(int i = 0; i < x - 1; i++){
    24         printf("%c", s[i]);
    25         for(int j = 0; j < y; j++){
    26             printf(" ");
    27         }
    28         printf("%c
    ", s[n - 1 - i]);
    29     }
    30     for(int i = x - 1; i <= x + y; i++){
    31         printf("%c", s[i]);
    32     }
    33     printf("
    ");
    34 
    35     return 0;
    36 }
  • 相关阅读:
    Javascript中的Math.max()和Math.min()
    附件预览项目采坑记
    移动设备后台的理解
    网络是怎么连接的?(进阶一)
    Git建立本地分支和远程分支的映射关系
    springboot使用redis实现发布与订阅
    centos下安装配置mongodb
    vue循环时设置多选框禁用状态,v-for
    谷歌浏览器postman插件安装,亲测可用
    element-ui 无法对绑定表单的对象中的对象属性进行验证
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9866897.html
Copyright © 2011-2022 走看看