首页 > 试题广场 >

Hello World for U

[编程题]Hello World for U
  • 热度指数:14339 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
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.

输入描述:
There are multiple test cases.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.


输出描述:
For each test case, print the input string in the shape of U as specified in the description.
示例1

输入

helloworld!
www.nowcoder.com

输出

h   !
e   d
l   l
lowor
w    m
w    o
w    c
.    .
n    r
owcode
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
    int n, h, w;
    char s[100];
    while (gets(s)) {
        n = strlen(s);
        h = n/3;
        w = n-2*h;
        if (w<=h) {
            h --;
            w += 2;
        }
        for (int i = 0; i < h; i ++) {
                printf("%c", s[i]);
                for(int j = 0; j < w-2;j++){
                    printf(" ");
                }
                printf("%c\n", s[n-i-1]);
        }
        for (int k = 0; k < w; k ++) {
            printf("%c", s[h+k]);
        }
        printf("\n");
    }
   
    return 0;
}

发表于 2023-03-24 20:26:15 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#define MAXSIZE 100

char character[MAXSIZE];

int Heigh(int count){
    int k = 0;
    for(int N = 3; N <= count; N++){
        if ((count - N) % 2 == 0){
            if ((count + 2 - N) / 2 <= N && (count + 2 - N) / 2 > k){
                k = (count + 2 - N) / 2;  
            }
        }
    }
    return k;
}

int Print(int h/*高*/, int d/*底宽*/, int length){
    char out[h][d + 1];
    for(int i = 0; i < h; i++){
        for(int j = 0; j <= d; j ++){
            out[i][j] = ' ';
        }
    }
    for (int i = 0; i < h; i++){
        out[i][0] = character[i];
        out[i][d - 1] = character[length - i - 1];
        out[i][d] = '\n';
    }
    for (int i = 1; i < d - 1; i++){
        out[h - 1][i] = character[h - 1 + i];
    }
    for(int i = 0; i < h; i++){
        for(int j = 0; j <= d; j ++){
            printf("%c",out[i][j]);
        }
    }
    return 0;
}

int main() {
    while (scanf("%s",character) != EOF) {
        int length = 0;
        for (length = 0; character[length] != '\0'; length++){}
        int n1 = 0, n2 = 0;
        int h = Heigh (length);
        int d = length + 2 - h * 2;
        Print(h, d, length );
    }
    return 0;
}





















发表于 2023-03-04 19:08:22 回复(1)
#include <stdio.h>
#include <string.h>

char matrix[80][80];

int main() {
    char str[80];
    while (scanf("%s", str) != EOF) { // 注意 while 处理多个 case
        int n = strlen(str);
        int n1 = (n+2)/3;
        int n3 = n1;
        int n2 = n+2-n1-n3;
        // 初始化矩阵
        for(int i=0;i<n1;i++){
            for(int j=0;j<n2;j++){
                matrix[i][j] = ' ';
            }
        }
        // 先填充n1
        for(int i=0;i<n1;i++){
            matrix[i][0] = str[i];
        }
        // 再填充n2
        for(int j=1;j<n2;j++){
            matrix[n1-1][j] = str[n1+j-1];
        }
        // 再填充n3
        for(int i=n3-2;i>=0;i--){
            matrix[i][n2-1] = str[n-1-i];
        }
        //  再填充其余部分
        for(int i=0;i<n1-2;i++){
            for(int j=1;j<n2-3;j++){
                matrix[i][j]=' ';
            }
        }
       
        for(int i=0;i<n1;i++){
            for(int j=0;j<n2;j++){
                printf("%c",matrix[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}
发表于 2023-02-19 16:12:08 回复(0)
这题的关键是在于n1+n2+n3=N+2&&n1=n3=(N+2)/3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    char str[81];
    while(scanf("%s",&str)!=EOF){
        int len=strlen(str);
        int n=(len+2)/3;
        for(int i=0;i<=n-1;i++){
            printf("%c",str[i]);
            for(int j=0;j<(len-2*n);j++){
                if(i!=n-1){
                    printf(" ");
                }
                else{
                    printf("%c",str[i+j+1]);
                }
            }
            printf("%c",str[len-1-i]);
            printf("\n");
        }
    }
    return 0;
}

发表于 2022-02-13 22:20:39 回复(0)
#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
    int len,r,c,remainder;
    int i,j;
    char str[100];
    char re[100][100];
    while(gets(str))
    {
        len=strlen(str);
        r=(len+2)/3;
        remainder=(len+2)%3;
        if(remainder!=0)
        {
            r=r+1;
            r=r+(len+2-r)%2;
            c=(len+2-r)/2;
        }
        else
        {
            r=r+(len+2-r)%2;
            c=(len+2-r)/2;
        }
        for(i=0;i<c-1;i++)
        {
            re[i][0]=str[i];
            re[i][r-1]=str[len-i-1];
            for(j=1;j<r-1;j++)
            {
                re[i][j]=' ';
            }
        }
        for(j=0;j<r;j++)
        {
            re[c-1][j]=str[c-1+j];
        }
        for(i=0;i<c;i++)
        {
            for(j=0;j<r;j++)
            {
                printf("%c",re[i][j]);
            }
            printf("\n");
        }
    }
}

发表于 2021-08-24 22:20:23 回复(0)

问题信息

难度:
5条回答 6424浏览

热门推荐

通过挑战的用户

查看代码