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 }
  • 相关阅读:
    ABP官方文档翻译 3.4 领域服务
    ABP官方文档翻译 3.3 仓储
    ABP官方文档翻译 3.2 值对象
    ABP官方文档翻译 3.1 实体
    ABP官方文档翻译 2.7 对象到对象的映射
    ABP官方文档翻译 2.6 定时
    bootstrap 源码中部分不了解的css属性
    vue 父组件调用子组件事件/子组件调用父组件方法
    js性能优化问题学习笔记
    js递归性能影响及解决方案
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9866897.html
Copyright © 2011-2022 走看看