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 }
  • 相关阅读:
    multiselect2side双向选择列表插件改进版
    通用权限管理平台--实现站点地图
    通用权限管理平台--系统日志
    通用权限案例平台--登录认证
    通用权限管理平台--代码结构
    通用权限管理平台--功能划分
    通用权限管理平台--架构选型
    tests
    Python 2.7_多进程获取简书专题数据(一)
    python2.7 爬取简书30日热门专题文章之简单分析_20170207
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9866897.html
Copyright © 2011-2022 走看看