1031. Hello World for U (20)

本文介绍了一种将任意长度(大于等于5)的字符串按U形打印的方法,确保字符顺序不变,同时使得U形尽可能接近正方形。文章提供了一个具体的实现示例,包括输入输出规范及样例。

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 <iostream>
#include <string>
using namespace std;

void print(int n1, int n2, int n3, string word)
{
    int counter = 0;
    char graph[n1][n2];
    for(int i=0; i<n1; i++)
    {
        for(int j=0; j<n2; j++)
            graph[i][j] = ' ';
    }
    for(int i=0; i<n1; i++)
        graph[i][0] = word[counter++];
    for(int i=1; i<n2-1; i++)
        graph[n1-1][i] = word[counter++];
    for(int i=n1-1; i>=0; i--)
        graph[i][n2-1] = word[counter++];
    for(int i=0; i<n1; i++)
    {
        for(int j=0; j<n2; j++)
            cout << graph[i][j];
        cout << endl;
    }
}
int main()
{
    int n1, n2, n3;
    string word;
    cin >> word;
    for(n2=3; n2<word.size(); n2++)
    {
        for(n1=0; n1<n2+1; n1++)
        {
            if(n1*2+n2-2 == word.size())
                break;
        }
        if(n1 != n2+1)
            break;
    }
    n1 = n3 = (word.size()+2-n2)/2;
    print(n1, n2, n3, word);
    return 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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值