A1031. Hello World for U (20)

A1031

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
我的AC代码如下:

#include <cstdio>
#include <cstring>
int main() {
    int x, y;
    char s[1000];
    int n;
    gets_s(s);
    n = strlen(s);
    int m = (n + 2) % 3;
    if (m != 0) {
        x = (n + 2) / 3 + m;
        y = (n + 2) / 3;
    }
    else {
        x = y = (n + 2) / 3;
    }
    //n2 = x;n1=n3=y;
    y--;
    int count = 0;
    for (int i = 0; i < y; i++) {
        printf("%c", s[count]);
        for (int j = 2; j < x; j++) {
            printf(" ");
        }
        printf("%c", s[n - count - 1]);
        printf("\n");
        count++;
    }
    for (int i = 0; i < x; i++) {
        printf("%c", s[count]);
        count++;
    }
    return 0;
}
注:此题利用找规律的方法做的,很巧妙。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 概述.............................................................................................................................................1 1.1. 自然语言&计算机语言................................................................................................1 1.2. 计算机语言 & C/C++语言..........................................................................................2 1.3. 简单的C/C++程序及其运行方法(环境的使用)................................................2 1.3.1. C/C++程序开发运行环境....................................................................................2 1.3.2. 格式输出函数printf()和格式输入函数scanf()....................................................3 1.4. 习题..............................................................................................................................5 2. 基本的C语言................................................................................................................................6 2.1. C语言中的名词——类型、量值(常量和变量)....................................................6 2.1.1. 整型和整型量值...................................................................................................6 2.1.2. 浮点型和浮点量(常量和变量).......................................................................8 2.1.3. 字符型和字符量(常量和变量).......................................................................9 2.1.4. 字符串常量.........................................................................................................10 2.2. C语言中的动词—运算符,短语-表达式 和和特殊动词性关键字....................11 2.2.1. 赋值运算符和赋值表达式.................................................................................11 2.2.2. 算术运算符和算术表达式.................................................................................12 2.2.3. 逻辑运算符和逻辑表达式.................................................................................13 2.2.4. 关系运算符和关系表达式.................................................................................14 2.2.5. 其它运算符和表达式.........................................................................................15 2.2.6. 不同类型量值的不同运算的混合.....................................................................17 2.3. C语言中的连词及句子——分支、循环和顺序语句..............................................19 2.3.1. if-else:如果-那么...........................................................................................19 2.3.2. switch-case语句..................................................................................................21 2.3.3. ?-:语句................................................................................................................23 2.3.4. for........................................................................................................................23 2.3.5. while....................................................................................................................24 2.3.6. do-while..............................................................................................................25 2.3.7. goto-label-if.........................................................................................................26 2.3.8. break....................................................................................................................27 2.3.9. continue...............................................................................................................28 2.4. C语言中的句子小结..................................................................................................28 2.5. 用C语言写一段话——程序段..................................................................................29 2.6. 用C语言写复杂段落——语句嵌套..........................................................................29 2.6.1. 复杂段落——语句嵌套的含义.........................................................................29 2.6.2. 嵌套语句的示例.................................................................................................30 2.6.3. 复杂段落——语句嵌套的小结.........................................................................33 2.7. 总结............................................................................................................................33 习题...........................................................................................................................................34 3. C语言的横向名词性扩充..........................................................................................................34 3.1. C语言的横向名词性扩充(1):同类变量的集合——数组..................................34

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值