1031. Hello World for U (20)

本文介绍了一个编程问题,即如何将给定的字符串以U形方式进行打印,并确保U形尽可能接近正方形。文章提供了完整的代码实现及输入输出示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1031. Hello World for U (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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| k <= n2 for all 3 <= n2 <= 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
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
    char s[100];
    scanf("%s", s);
    int len = strlen(s);
    int n = (len + 2) / 3;
    int m = len - 2*n;
    int l = 0, r = len - 1;
   for(int i = 0; i < n - 1; i++)
   {
       printf("%c",s[l]);
       for(int j = 0; j < m; j++)
        printf(" ");
       printf("%c\n",s[r]);
       l++;
       r--;
   }
      for(int i = 0; i < m+2; i++)
    printf("%c", s[l++]);

}


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、付费专栏及课程。

余额充值