PAT_A1031 | Hello World for U

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 

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

在这里插入图片描述

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值