首页 > 试题广场 >

Hello World for U

[编程题]Hello World for U
  • 热度指数:16712 时间限制: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 <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    while (cin>>str) {
        int n = str.length();
        int n1 = (n + 2) / 3;
        int n2 = n - n1*2 + 2;
        for (int i = 0; i < n1 - 1; i++) {
            cout<<str[i];
            for (int i = 0; i < n2 - 2; i++) {
                cout<<' ';
            }
            cout<<str[n-1-i]<<endl;
        }

        for (int i = -1; i < n2 - 1; i++) {
            cout<<str[n1+i];
        }
        cout<<endl;
    }
    return 0;
}

发表于 2019-12-28 18:02:38 回复(0)
这道题我必须留下讨论,评论里没一个能指出这题给的自测样例有问题的吗?自测一直提示我的输出格式不符合要求,但实际上跟自测的样例没任何差别,事实上直接判题也是可以过的,害的我自测了半天,看前人评论也没个指出的.....
这道题就是鸡兔同笼的变形加图形输出题,题意就是解2x+y-2=N这个方程,x必须小于等于y,这样有三种情况,x=y,x=y-1,x=y-2(x不可能等于y-3,因为那样就和x=y情况等价了)
#include<bits/stdc++.h>
using namespace std;
int main(){
    string str;
    while(getline(cin,str)){
        int n=str.length();
        int x,y;
        if((n+2)%3==0){
            x=y=(n+2)/3;
        }else if((n+4)%3==0){
            y=(n+4)/3;
            x=y-1;
        }else if((n+6)%3==0){
            y=(n+6)/3;
            x=y-2;
        }
        for(int i=0;i<x-1;i++){
            cout<<str[i];
            for(int j=0;j<y-2;j++)cout<<" ";
            cout<<str[n-i-1]<<endl;
            //cout<<endl;
        }
        for(int i=x-1;i<=n-x;i++)cout<<str[i];
        cout<<endl;
    }
    return 0;
}



发表于 2020-03-30 14:58:21 回复(4)
第二次做这个题,更新:
n1=n3=(s.size()+2)/3;
n2=(s.size()+2)-(s.size()+2)/3*2;
加2是因为中间会重叠两个
#include<iostream>
(720)#include<string>
using namespace std;
int main(){
    string s;
    while(cin>>s){
        int len=s.size();
        len+=2;
        int l=len/3-1;//(len+2)/3就是n1和n2的值
        int mid=len-len/3*2;
        for(int i=0;i<l;i++){
            cout<<s[i];
            for(int j=0;j<mid-2;j++)cout<<" ";
            cout<<s[s.size()-1-i]<<endl;
        }for(int i=l;i<l+mid;i++){
            cout<<s[i];
        }
    }
    return 0;
}


发表于 2020-03-12 19:08:53 回复(0)
这道题可以根据给出的字符串总长度,计算出n1、n2、n3的值。有了这几个值,就可以直到输出所需要的行数,列数,中间空格的个数。就很容易做出答案。
#include<iostream>
(720)#include<string>
using namespace std;

int main(){
	string str;
	while(cin>>str){
		int N=str.length();
		int n2 = (N+2)%3==0 ? (N+2)/3 : (N+2)/3+1;
		if( (n2%2==0 && N%2==1) || (n2%2==1 && N%2==0)) n2++;
		int n1 = (N-n2)/2;//不包括与n2重叠的
		int n3 = (N-n2)/2;//不包括与n2重叠的
		//共需要n1+1行,n2列
		int blank_number = n2-2;
		int index_first=0;
		int index_last=N-1;
		for(int i=0;i<n1;i++){
			int temp_blank=blank_number;
			cout<<str[index_first++];
			while(temp_blank-- > 0) cout<<" ";
			cout<<str[index_last--]<<endl; 
		}  
		for(int i=0;i<n2;i++){
			cout<<str[index_first++];
		}
	}
	return 0;
}


发表于 2020-03-23 22:54:08 回复(0)

#include<cstdio>
#include<string.h>
using namespace std;

int main(){
	char str[80];
	while(~scanf("%s",str)){
		int i, n1=0;
		int N=strlen(str);
		for(i=1;i<N;i++){                   /*先计算n1和n2*/
			if(N+2-2*i>=i && i>n1)
				n1=i;
		}
		int n2=N+2-2*n1;
		
		int j=N, k;                         /*按行输出*/ 
		for(i=0,j=N-1;j-i+1>n2;i++,j--){
			printf("%c",str[i]);           /*输出每行第一个字符*/ 
			for(k=0;k<n2-2;k++)            /*输出中间的空格*/ 
				printf(" ");
			printf("%c\n",str[j]);         /*输出每行最后一个字符*/ 
		} 
		for(;i<=j;i++)                     /*输出最后一行*/ 
			printf("%c",str[i]);
		printf("\n");			
	}
	return 0;
}

发表于 2020-02-10 12:36:30 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    while(cin>>s)
    {
        int n=s.length(),n1=(n+2)/3,n2=n+2-2*n1;
        for(int i=0;i<n1-1;++i)
        {
            cout<<s[i];
            for(int j=0;j<n2-2;++j)
                cout<<" ";
            cout<<s[n-i-1]<<endl;
        }
        cout<<s.substr(n1-1,n2)<<endl;
    }
    return 0;
}
  

简单控制输入输出即可
发表于 2021-01-28 23:41:56 回复(0)
#Python实现,将字符串平均分为三份,如果有余数的都给横排。
#如果余数为零就要分3个给最下方,因为竖排不能比横排多,余数为零表示竖排比横排多一
try:
    while True:
        string = list(input())
        a,b = divmod(len(string),3)   #得到除数和余数
        if b<1:                     #如果余数为0
            a-=1
            b+=3
        temp = " "*(a+b-2)      #得到中间的空格数
        for i in range(a):
            print(string.pop(0)+temp+string.pop(-1))  #输出头和尾
        print("".join(string))       #最后一行剩余的当做一个字符串输出
except Exception:
    pass

编辑于 2018-09-20 15:12:15 回复(0)
#include <cstring>
#include <iostream>
using namespace std;
char s1[20][20];
int main() {
    string s2;
    cin>>s2;
    int length = s2.length();
    int h = (length + 2) / 3;
    int c = length - 2 * (h - 1);
    int k1 = 0;
    for(int j=0;j<c;j++)//列
    {
        for(int i=0;i<h;i++)//行
        {
            if(j==0 || i==h-1)
            {
                s1[i][j]=s2[k1];
                k1++;
            }
            else
            {
                s1[i][j]=' ';
            }
        }
    }
    int k2=length-1;
    for(int i=0;i<h;i++)
    {
        s1[i][c-1]=s2[k2];
        k2--;
    }
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < c; j++)
        {
            printf("%c",s1[i][j]);
        }
        printf("\n");
    }
    return 0;
}
编辑于 2024-01-15 16:44:03 回复(0)
  1. 思路:首先计算n1,n2,n3值,n1=n3,n2=N+2-2*n1 利用暴力求解法,求出最大满足题意的n1
  2. 运用一个二维数组,依次保存第1列,第2~n2-1列,以及最后一列的数据,保存方向从下至上呈U型,只有2~n2-1列需要填充空格
  3. 输出二维数组
#include <iostream>
using namespace std;

int main() {
    string a;
    cin>>a;
    int N=a.length();
    int n1,n2;
    for(int j=(N+2)/2;j>=0;j--){//解n1值
        n2=N+2-2*j;
        if(n2>=j) {
            n1=j;
            break;
        }//解n2值且需满足条件
    }
    char martix[100][100];
    for(int cow=0;cow<n1;cow++){
        martix[cow][0]=a[cow];
    }//第一列
    for(int column=1;column<n2-1;column++){
        for(int row=0;row<n1;row++){
            if(row!=n1-1){
                martix[row][column]=' ';
            }
            else{
                martix[row][column]=a[n1+column-1];
            }
        }
    }

    int count=0;
    for(int row=n1-1;row>=0;row--){
        martix[row][n2-1]=a[n1-1+n2-1+count];
        count++;
    }//最后一列
   // cout<<martix[0][0]<<"\n";
    for(int row=0;row<n1;row++){
        for(int column=0;column<n2;column++){
            cout<<martix[row][column];
        }
        cout<<"\n";
    }
    return 0;

}


发表于 2023-01-10 21:38:35 回复(0)
这样的解法为什么“darzowk”不符合?

#include
<iostream>
#include<string>
using namespace std;
int main() {
    string s;
    while (getline(cin, s)) {
        int l = s.length();
        int n1, n2, n3;
        int i;
        for (i = 3;i < l;i++) {
            n1 = (l + 2 - i) / 2;
            if (n1 < i && (l + 2 - i) % 2 == 0)
                break;
        }
        n3 = n1;
        n2 = i;
        char a[100][100];
        for (int m = 0;m < 100;m++) {
            for (int n = 0;n < 100;n++) {
                a[m][n] = ' ';
            }
        }
        int j = 0;
        for (int i = 0;i < n1 - 1;i++) {
            a[i][0] = s[j];
            j++;
        }
        for (int i = 0;i < n2;i++) {
            a[n1 - 1][i] = s[j];
            j++;
        }
        for (int i = n1 - 2;i >= 0;i--) {
            a[i][n2 - 1] = s[j];
            j++;
        }
        for (int i = 0;i < n1;i++) {
            for (int k = 0;k < n2;k++)
                cout << a[i][k];
            cout << endl;
        }
    }
}
发表于 2025-03-01 12:49:09 回复(0)
#include <stdio.h>
#include <string.h>
int main() {
    char str[100]={0};
    while(scanf("%s",str) !=EOF)
    {
        int width; //底边宽
        int high;  //两侧高
        int N=strlen(str);
        //N=2*high+width-2
        //width>=3,width>=high,故N>=5
        if(N<5) printf("%s",str);
        else
         {
            if((N+2)%3==0)  //high=width
            {
                high = width = (N+2)/3;
            }
            else    //high != width
            {
                high =(N+2)/3;
                width = high + (N+2)%3;
            }
            int i;
            for(i=0;i<high-1;i++)    
            {
                printf("%c",str[i]);    //竖着拼写
                for(int j=1;j<width-1;j++)
                {
                    printf(" ");      //横着打空格
                }
                printf("%c\n",str[N-i-1]);      //最后一列倒着输出且换行
            }
            for(i=high-1;i<width+high-1;i++)
            {
                printf("%c",str[i]);          //最后一行完整输出
            }
            printf("\n");    //连续输出
         };
    }
    return 0;
}
发表于 2025-02-26 21:39:26 回复(0)
def U(s, n):
    n1 = n2  = n // 3
    r = n % 3
    if r == 0:
        n1 -= 1
        n2 -= 1
    n3 = n - 2 * n1
    m1 = s[:n1]
    m3 = s[n1:n3+n1]
    m2 = s[n3+n1:n]
    c = 0
    i = 1
    for _ in range(n1):
        print(m1[c], end='')
        for _ in range(n3-2):
            print(' ', end='')
   
        print(m2[-i])
        i += 1
        c += 1
    print(m3)

    

while True:
    try:
        s = input()
        n = len(s)
        U(s, n)
    except:
        break

发表于 2025-02-25 16:59:15 回复(0)
//按行输出,U上部分插入空格
#include "stdio.h"
#include "string.h"
using namespace std;
int main() {
    char str[100] = { 0 };
    while (scanf("%s", str) != EOF) {
        int N = strlen(str);
        int n1, n2, n3;
        if (N < 5) {
            for (int i = 0;i < N;i++)
            {
                printf("%c", str[i]);
            }
            printf("\n");
        }
        else {
            if ((N + 2) % 3 == 0)
                n1 = n2 = n3 = (N + 2) / 3;
            else {
                n1 = n3 = (N+2)  / 3 ;
                n2 = (N+2)/3+(N+2)%3;
            }
            for (int i = 0;i < n1 - 1;i++) {
                printf("%c", str[i]);
                for (int j = 1;j < n2 - 1;j++) {
                    printf(" ");
                }
                printf("%c\n", str[N - 1 - i]);
            }
            for (int i = n1 - 1;i < n1 + n2 - 1;i++)
                printf("%c", str[i]);
            printf("\n");
            memset(str, 0, 100);
        }
    }
    return 0;
}

发表于 2025-01-18 21:52:47 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main() {
    string s;
    while (cin >> s) { 
        int len = s.length();
        len += 2;
        int n1 = len / 3;
        int n2 = len - n1 * 2;
        n2 -= 2;
        for (int i = 0; i < n1; i++) {
            cout << s[i];
            if (i != n1 - 1) {
                for (int j = 0; j < n2; j++) {
                    cout << " ";
                }
            } else {
                for (int j = 0; j < n2; j++)
                    cout << s[n1 + j];

            }
            cout << s[len - 3 - i] << endl;

        }

    }
}

发表于 2024-12-29 14:24:38 回复(0)
#include <stdio.h>
#include <string.h>
using namespace std;
int main(){
    char data[1000];//保存用户输入
    char tmp[100][100]={0};//用于打印输出
    while(scanf("%s",&data)!=EOF){
        memset(tmp,0,10000);
        int n=strlen(data);
        // printf("%d\n",n);
        int n1=0,n2=0,n3=0;
        for(int i=n;i>=3;i--){
            if((n-i+2)%2==0&&(n-i+2)/2<=i){
                n1=n3=(n-i+2)/2;
                n2=i;
            }
        }
        for(int i=0;i<n1;i++){
            for(int j=0;j<n2;j++){
                tmp[i][j]=' ';
            }
        }
        // printf("%d %d %d\n",n1,n2,n3);
        int i;
        for(i=0;i<n1;i++){
            tmp[i][0]=data[i];
        }
        // printf("%d\n",i);
        for(int j=1;j<n2;j++){
            tmp[n1-1][j]=data[i];
            i++;
        }
        for(int k=n1-2;k>=0;k--){
            tmp[k][n2-1]=data[i];
            i++;
        }
        for(int l=0;l<n1;l++){
            printf("%s\n",tmp[l]);
        }

    }
    return 0;
}
发表于 2024-12-27 16:16:56 回复(0)
#include <iostream>
using namespace std;

int main() {
    string temp;
    int len, width,bottom;
    while (true) {
        getline(cin, temp);
        len = temp.size();
        // cout << "the string size:" << len << endl;
        if (!len)return 0;
        if (len % 3 == 2)
            width = (len + 1) / 3;
        else if(len%3 ==1)width = (len + 2) / 3;
        else width=len/3;
        bottom=len-2*width+2;
        // cout << "the width:" << width << endl;
        for (int i = 0; i < width; i++) {
            if (i != width - 1)//不是最后一行
                for (int j = 0; j < bottom; j++) {
                    if (j == 0)cout << temp[i];
                    else if (j == bottom-1 )cout << temp[len - i-1];
                    else cout << ' ';
                } 
            else {
                for (int k = 0;i + k < len - width+1; k++) {
                    cout << temp[i + k];
                }
                }
            cout << endl;
        }

    }
}
// 64 位输出请用 printf("%lld")

编辑于 2024-03-19 20:54:54 回复(0)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
    string str;
    while(cin >> str){
        int len=(str.size()+2);
        vector<char> vec(len/3+1);
        int idx=0;
        int cnt;
        if(len%3==0) cnt=len/3-1;
        else if(len%3==1) cnt=len/3;
        else cnt=len/3+1;
        for(int i=0; i<len/3-1; ++i) {
            cout << str[idx];
            for (int j = 1; j < cnt; ++j) {
                cout << ' ';
            }
            cout << str[str.size() - 1 - idx] << endl;
            idx++;
        }
        for(int i=idx; i<str.size()-idx; ++i){
            cout << str[i];
        }
        cout << endl;
    }
}

发表于 2024-03-07 21:37:55 回复(0)
#include <cstdio>
using namespace std;
int main() {
    char str[81];//最长字符长80
    int N, n1, n2, n3;

    while (scanf("%s", str) != EOF) {

        int k = 0;
        while (str[k] != 0) ++k;
        N = k;
        //给定N,求n1和n2(n3=n1),2n1+n2=N+2,题意为使n1和n2尽量接近,但n2大于等于n1
        n1 = (N + 2) / 3;
        n2 = N + 2 - 2 * n1;
        n3 = n1;
        for (int i = 0; i < n1 - 1; ++i) {
            for (int j = 0; j < n2; ++j) {
                if (j == 0) printf("%c", str[i]);
                else if (j == (n2 - 1)) printf("%c", str[N - 1 - i]);
                else printf(" ");
            }
            printf("\n");//换行
        }
        //输出最后一行
        for (int j = 0; j < n2; ++j) printf("%c", str[n1 - 1 + j]);
        //输出下一个需要换行
        printf("\n");
    }
    return 0;
}



编辑于 2024-03-05 16:50:40 回复(0)
#include <iostream>
using namespace std;

int main() {
    string s;
    while (cin >>s) { // 注意 while 处理多个 case
        int n=0;
        int len=s.size();
        if(len%3==0){
            n=len/3 -1;
        }else{
            n=len/3;
        }
       
        int rest=len-2*n;
        for(int i=0;i<n;i++){
            cout<<s[i];
            for(int j=0;j<rest-2;j++){
                cout<<' ';
            }
            cout<<s[len-i-1]<<endl;
        }
        for(int i=n;i<len-n;i++){
            cout<<s[i];
        }
        cout<<endl;
    }
}
// 64 位输出请用 printf("%lld")
发表于 2024-03-02 16:14:22 回复(0)
/*
描述
给定任意一个N(>=5)个字符的字符串,您将被要求将字符形成U的形状。
例如,“helloworld”可以打印为:h d e l l r lowo
也就是说,字符必须按原始顺序打印,从左垂直行自上而下开始,共有n1个字符,
然后沿底行从左向右打印,共有n2个字符,最后沿着具有n3个字符的垂直线自下而上。
而且,我们希望U尽可能方——也就是说,
它必须满足n1=n3=max{k|k<=n2,对于n1+n2+n3-2=N的所有3<=n2<=N}。
输入描述:
有多个测试用例。每个用例包含一个字符串,
一行中不少于5个字符,不超过80个字符。该字符串不包含空白。
输出描述:
对于每个测试用例,将输入字符串打印为描述中指定的U形。
*/
#include <array>
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    while (cin >> str) {
        int n = str.size();
        int n1, n2, n3;
        n1 = n3 = (n + 2) / 3;
        n2 = n + 2 - n1 - n3;
        array<array<char, 80>, 80>matrix;
        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
                matrix[i][j] = ' ';
            }
        }
        for (int i = 0; i < n1; i++) {
            matrix[i][0] = str[i];
            matrix[i][n2 - 1] = str[n - 1 - i];
        }
        for (int i = n1, count = 1; count <= n2 - 2; i++, count++) {
            matrix[n1 - 1][count] = str[i];
        }
        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
                cout << matrix[i][j];
            }
            cout << endl;
        }
    }
    return 0;
}

编辑于 2024-02-02 18:20:12 回复(0)