欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PAT_A1031 | Hello World for U

程序员文章站 2024-03-07 15:45:33
...

1031 Hello World for U (20point(s))

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、先根据输入的字符串计算字符的个数

可以直接用 "cstring" 库中的 strlen 函数计算字符串长度,即字符个数,记为len。

需要注意的是,字符串数组从下标 0 开始到len -1 截止

 

2、根据字符个数计算u型的高度

将u型拆解为两部分认识:两条高和一条底边,其中公用了两个点

那么我们记高为 h,底边宽为 width,那么有:

len = width + 2 * h - 2

h = (len + 2 - width)/ 2

根据题意,h必须为整数,所以(len + 2 - width)必须能整除 2 才行

根据题目要求:

  • h <= len
  • len >= 3
  • (len + 2 - width)% 2 == 0

 那么弄清楚这些条件,求h就很好办了。

代码如下:

/* 计算u型的高
 * 传入参数:
 * 字符的个数
 * 返回:
 * 符合题意的u型的高 */
int calc_h(int n) {
    int width = 2, h;

    do {
        width++;

        while ((n + 2 - width) % 2 != 0)
            width++;

        h = (n + 2 - width) / 2;
    } while (h > width);

    return h;
}

 3、寻找输出规律

???? 先打印两条高(不包括最底端)

先打印左边字符、再打印width - 2 个空格、再打印右边字符。

  • 左边字符:字符串数组下标 从 0 到 h - 2
  • 右边字符:字符串数组下标 从 len - 1 到 len - h - 3

再具体一点,需要打印 h - 1 行,默认行数从 0 开始计数,则第 i 行:

  • 左边字符:字符串数组下标 为 i
  • 右边字符:字符串数组下标 从 len - 1 - i

 

???? 再从左至右完整地打印底边

字符串数组下标从 h - 1 开始,一共打印 width 个字符 。

 


完整代码如下:

//
// Created by LittleCat on 2020/2/1.
//

#include <cstdio>
#include <cstring>

#define MAX 82

/* 计算u型的高
 * 传入参数:
 * 字符的个数
 * 返回:
 * 符合题意的u型的高 */
int calc_h(int n) {
    int width = 2, h;

    do {
        width++;

        while ((n + 2 - width) % 2 != 0)
            width++;

        h = (n + 2 - width) / 2;
    } while (h > width);

    return h;
}

void printBlank(int n) {
    for (int i = 0; i < n; i++)
        printf(" ");
}

int main() {
    char str[MAX];
    scanf("%s", str);

    int len = strlen(str);
    int h = calc_h(len);
    int width = len + 2 - 2 * h;

    /* 打印两条高(不包括最后一行的部分)*/
    for (int i = 0; i < h - 1; i++) {
        printf("%c", str[i]);
        printBlank(width - 2);
        printf("%c\n", str[len - 1 - i]);
    }

    /* 打印底边 */
    for (int i = 0; i < width; i++)
        printf("%c", str[h - 1 + i]);

    printf("\n");
    return 0;
}


end 

欢迎关注个人公众号“鸡翅编程”,这里是认真且乖巧的码农一枚,旨在用心写好每一篇文章,平常会把笔记汇总成推送更新~
 

PAT_A1031 | Hello World for U

 

相关标签: # 图形输出