首页 > 试题广场 >

Hello World for U

[编程题]Hello World for U
  • 热度指数:14294 时间限制: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 回复(2)
第二次做这个题,更新:
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)
#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)
#include <bits/stdc++.h>

using namespace std;

int main() {
    string str;
    while (cin >> str) {
        int N = str.length();
        int n1 = (N+2)/3, n2 = N-n1*2+2;
        int left = 0, right = N - 1;
        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
                if (j == 0 || i == n1-1) {a
                    cout << str.substr(left, 1);
                    left++;
                }
                else if (j == n2 - 1 && right > left) {
                    cout << str.substr(right, 1);
                    right--;
                }
                else {
                    cout << ' ';
                }
            }
            cout << endl;
        }
    }
    return 0;
}


发表于 2023-08-13 15:58:30 回复(0)
#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 <iostream>
using namespace std;

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

        }
    }
}

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


发表于 2023-02-25 11:33:42 回复(0)
#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)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
    int n, h, w;
    char s[100];
    fgets(s, 100, stdin);
    n = strlen(s)-1;
    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]);
    }
    return 0;
}

发表于 2023-02-03 16:47:59 回复(0)