首页 > 试题广场 >

Hello World for U (20)

[编程题]Hello World for U (20)
  • 热度指数:2477 时间限制: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.

输入描述:
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.


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

输入

helloworld!

输出

h   !<br/>e   d<br/>l   l<br/>lowor

#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
 
int main()
{
    char sen[100];
    int i = 0, j = 0, k = 0;
    cin >> sen;
    int len = strlen(sen);
    int bian = (len + 2) / 3; //为了成为矩形,尽量平均
    int ju = len - bian * 2;  //最下面的边除两端有多长
    bian--;
    //输出除底边外的上两边
    for (i = 0; i < bian; i++)
    {
        cout << sen[i] << setw(ju) << " "<< sen[len - 1 - i] << endl;
    }
    j = len - 1 - i;
    //输出最后一行
    for (i; i <= j; i++)
    cout << sen[i];
    return 0;
}

编辑于 2018-08-14 11:28:57 回复(0)
/*
https://www.nowcoder.com/pat/5/problem/4018
Hello World for U (20)  2018-12-20
General mean: give you a string a, print the pattern that like U to be as squared as possible .
Result: Cost about 30 minues, I suppose the n1 and n2 is the length of vertical bar
and horizontal bar of the "U". But that is not easy to find the algorithm calculate
the n1 and n2 and apply to all kind of input. So It type of problem need to try as more as example before submit your code.and among it time you should try to find the regular apply to all the case and the ways to solve it.
*/
#include<iostream>
#include<string>
using namespace std;
int main(){
string input;
cin >> input;
int length = input.length();
int n2 = 3, n1=0;    //suppose n1 is the vertical bar's length of "U" and n2 is the horizontal bar's length
while (true){
int res = length - n2;
if (res % 2 == 0){
n1 = res / 2;
if (n1 <= n2-1) break;
}
n2++;
}
for (int i = 0; i < n1; i++){
cout << input[i];
for (int j = 0; j < n2 - 2; j++) cout << " ";
cout << input[length-1-i] << endl;
}
for (int i = n1; i < n1+n2; i++) cout << input[i];
cout << endl;
return 0;
}
编辑于 2018-12-20 15:02:12 回复(0)
因为字符串长度不超过80,所以n1、n2、n3的长度可以用暴力解法求解


package go.jacob.day1128;

import java.util.Scanner;

/**
 * 
 * @author Administrator
 * 1031. Hello World for U (20)
 */
public class Demo2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        int num = s.length();
        int n1, n3 = num;
        for (n1 = 2; n1 < num; n1++) {
            n3 = num + 2 - 2 * n1;
            if (n3 < n1) {
                n1--;
                n3 = num + 2 - 2 * n1;
                break;
            }

        }
        int head = 0, tail = num - 1;
        for (int i = 0; i < n1 - 1; i++) {
            for (int j = 0; j < n3; j++) {
                if (j == 0)
                    System.out.print(s.charAt(head++));
                else if (j == n3 - 1)
                    System.out.print(s.charAt(tail--));
                else
                    System.out.print(' ');

            }
            System.out.println();
        }
        for (int j = 0; j < n3; j++) {
            System.out.print(s.charAt(head++));
        }
        sc.close();

    }
}

发表于 2017-11-28 10:16:11 回复(0)
#include <stdio.h>
#include <string.h>

char str[100];

void fun(int n)
{
	int side,bottom;
	int i,j;
	bottom = 3;
	
    for(i=bottom;i<=n;i++) //寻找满足条件的最大的侧边 
    {
    	if((n-i)%2 == 0)  //总数减去底边数,必须是偶数 
    	{
    		side = (n-i)/2 + 1;   //侧边数 
    		bottom = i;       //底边数 
    		if(bottom >= side)   //需满足条件 
    		{
    			//printf("bottom=%d,side=%d\n",bottom,side);
    			break;
    		}
    	}
    }
	
	
	for(i=0;i<side-1;i++)  //打印侧边 
	{
        printf("%c",str[i]);
        for(j=0;j<bottom-2;j++)
        {
        	printf(" ");
        }
        printf("%c",str[n-i-1]);
        printf("\n");
	}
	
	for(i=side-1;i<n-side+1;i++)  //打印底边 
	{
		printf("%c",str[i]);
	}
}

int main()
{
    int len;
    while(~scanf("%s",str))
    {
	    len = strlen(str);
	    fun(len);
    }

    return 0;
} 

发表于 2017-03-11 22:47:06 回复(0)
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {   	
        Scanner in=new Scanner(System.in);        
        char[] a=in.next().toCharArray();
        int h=a.length/3;//为打印结果高度-1
        if(a.length%3==0){
        	h--;
        }
        int w=a.length-2*h-2; //U行间的空格数
        int i=0,j=a.length-1;
    	
        while(i<h){//输出打印结果的前n-1行
	    	System.out.print(a[i]);
	    	for(int k=0;k<w;k++){
	    		System.out.print(" ");
	    	}
	    	System.out.println(a[j]);
	    	i++;
	    	j--;
        }
        for(int f=h;f<h+w+2;f++){//输出最后一行
        	System.out.print(a[f]);
        }
    }
}

发表于 2017-01-22 21:03:49 回复(0)
尽可能接近正方形,即长宽差最小,尽可能去相等,如果没法相等,根据题意知底边可以长一些。
#include<iostream>
using namespace std;

int main() {
	string s;
	int N,r,n1,n2;
	cin>>s;
	N=s.size();
	r=(N+2)%3;
	n1=(N+2)/3;
	n2=n1+r;
	for(int i=0;i<n1-1;i++){
		cout<<s[i];
		int j=n2-2;
		while(j--)cout<<" ";
		cout<<s[N-1-i]<<endl;
	}
	for(int i=n1-1;i<n2+n1-1;i++){
		cout<<s[i];
	}
	return 0;
}


发表于 2020-01-31 13:43:43 回复(1)
#include <iostream>
#include <string>

using namespace std;

int main() {
    string str;
    getline(cin, str);
    // n:底行长  n1:侧行长
    int n, n1, len = str.length();
    // 求底行长和侧行长
    for (int i = len + 1; i >= 3; i--) {
        n = i;
        n1 = n;

        if (n1 * 2 + n - 2 == len) break;
        n1 -= 1;
        if (n1 * 2 + n - 2 == len) break;
        n1 -= 1;
        if (n1 * 2 + n - 2 == len) break;
    }

    // 输出非底行
    for (int i = 0; i < n1 - 1; i++) {
        printf("%c", str[i]);
        // 输出空格
        for (int j = 0; j < n - 2; j++) printf(" ");
        printf("%c\n", str[len - 1 - i]);
    }

    // 输出底行
    for (int i = n1 - 1; i < n1 + n - 1; i++) printf("%c", str[i]);

    return 0;
}

发表于 2023-03-20 11:24:36 回复(0)
#include <iostream>
#include <string.h>

using namespace std;




int main()
{
    string str;
    while(getline(cin,str))
    {

        int N=str.length();
        int N2=0;
        int N3=0;
        int N1=0;
        for(int n2=N; (N+2<=3*n2) && n2>0; n2-=2)
        {
            N2=n2;
        }

        N1=(N+2-N2)/2;
        N3=N1;

        char  pic [N1][N2];
        memset(pic,' ',N1*N2);
        for(int i=0;i<N1;i++)
        {
            pic[i][0]=str[i];


        }
        for(int i=0;i<N2;i++)
        {
            pic[N1-1][i]=str[N1-1+i];

        }
        for(int i=N3-1;i>=0;i--)
        {
            pic[i][N2-1]=str[N1-1+N2-1+N3-1-i];

        }
        for(int i=0;i<N1;i++)
        {
            for(int j=0;j<N2;j++)
            {
                cout<<pic[i][j];
            }
          cout<<endl;

        }

       str="";

    }



    return 0;
}

发表于 2021-02-05 15:51:51 回复(0)
注意,当n1和n2的差值已经是最小(U的形状足够的“方”)时,还要找n2最大的情况!
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<bits/stdc++.h>

#define INF 2147483647
#define MIN INF+1
#define ll long long

using namespace std;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    string s;
    cin >> s;

    int min_diff = INF;
    int best_n1;
    int best_n2;
    for (int i = 3; i <= s.size(); ++i) {
        int n1 = (s.size() - i + 2) / 2;

        if(abs(n1 - i) < min_diff) {
            min_diff = abs(n1 - i);
            best_n1 = n1;
            best_n2 = s.size() - n1 * 2 + 2;
        }
        // it must be satisfied that n1 = n3 = max { k | k ≤ n2  for all 3 ≤ n2 ≤ N } with n1 + n2 + n3 − 2 = N.
        else if (abs(n1 - i) == min_diff) {
            if(best_n2 < s.size() - n1 * 2 + 2) {
                best_n2 = s.size() - n1 * 2 + 2;
                best_n1 = n1;
            }
        }

    }

    char m[best_n1][best_n2];

    for (int k = 0; k < best_n1; ++k) {
        for (int i = 0; i < best_n2; ++i) {
            m[k][i] = ' ';
        }
    }

    int nowy = 0, nowx = 0;     // 横纵坐标
    int do1 = 1, do2 = 0, do3 = 0;
    for (int j = 0; j < s.size(); ++j) {
        m[nowy][nowx] = s[j];
        if(do1) {
            if(nowy + 1 == best_n1) {
                do2 = 1;
                do1 = 0;
            } else {
                nowy++;
            }
        }
        if(do2) {
            if(nowx + 1 == best_n2) {
                do3 = 1;
                do2 = 0;
            } else {
                nowx++;
            }
        }
        if(do3) {
            nowy--;
        }
    }

    for (int k = 0; k < best_n1; ++k) {
        for (int i = 0; i < best_n2; ++i) {
            cout << m[k][i];
        }
        cout << endl;
    }

    return 0;
}


发表于 2020-09-19 20:44:36 回复(0)
#include<iostream>
(720)#include<vector>
#include<string>
using namespace std;
int main(){
    string s;
    cin>>s;
    int i=3,n1,n2,j;
    while(i<s.size()){
        int tmp=s.size()-i+2;
        if(tmp%2==0&&tmp/2<=i){
            n2=i;
            n1=tmp/2;
            break;
        }
        i++;
    }
    i=0;
    j=s.size()-1;
    while(i<n1-1){
        printf("%c",s[i]);
        for(int k=0;k<n2-2;k++){
            printf(" ");
        }
        printf("%c",s[j]);
        printf("\n");
        i++;
        j--;
    }
    for(;i<=j;i++){
        printf("%c",s[i]);
    }
}

编辑于 2020-04-16 21:32:18 回复(0)
#include<iostream>
#include<string>
using namespace std;

void helloworldforu() {
	string s;
	cin >> s;
	int n1, n2, n3;
	int N=s.size();
	n1 = n3 = (N + 2) / 3;
	n2 = N+2 - (n1 + n3);
	for (int i = 0; i < n1-1; i++) {
		cout << *s.begin();
		s.erase(s.begin());
		for (int i = 0; i < (n2 - 2); i++) {
			cout << " ";
		}
		cout << *(s.end() - 1) << endl;
		s.erase(s.end() - 1);
	}
	for (int i = 0; i < n2; i++) {
		cout << *s.begin();
		s.erase(s.begin());
	}

}//helloworld!
int main()
{
	 helloworldforu();
	return 0;
}

发表于 2020-01-21 16:10:03 回复(0)

这题我的思路主要受限于是那个每个块的长度搜索直接暴力遍历得到用了些时间,其余的就是将字符打印出来

#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;

int main() {
    string str;
    int n1 = 0, n2 = 0,tem = 0;
    cin >> str;
    for (int i = str.length(); i >= 3; i--) {
        for (int j = i; j >= 0; j--) {
            if (2 * j + i - 2 == str.length()) {
                tem = j > tem ? j : tem;
            }
        }
    }
    n1 = tem;
    n2 = str.length() - 2 * n1;
    for (int i = 0; i <n1-1; i++) {
        cout << str[i];
        for (int j = 0; j < n2; j++)cout << " ";
        cout << str[str.length() - i - 1] << endl;
    }
    for (int i = n1 - 1; i <n1+ n2 + 1; i++)cout << str[i];
    cout << endl;
    system("pause");
    return 0;
}
发表于 2019-10-10 23:59:25 回复(0)
有点傻 最开始忽略了条件就一直报错。。。
//n1 = n3 = max{k|k <= n2 for all 3<= n2 <= N}
#include <iostream>
#include <string>
using namespace std;

const int maxn = 1001;
char N1[maxn],N2[maxn],N3[maxn];
char S[maxn][maxn];

int main(){
    string s;
    cin >> s;
    int n = s.size();
    int length1,length2,length3;
    for(int i = 3;i <= n;i++){//n2长度
        if(n - ((n - i) / 2 + i) == (n - i) / 2 && (n - i) / 2 + 1 <= i){
            length1 = length3 = (n - i) / 2;
            length2 = i;
            break;
        }
    }
    
    for(int i = 0;i < length1;i++){
        N1[i] = s[i];
    }
    
    int k = 0;
    for(int i = n - 1;i >= n - length3;i--){
        N3[k] = s[i];
        k++;
    }
    
    int l = 0;
    for(int i = length1;i < n - length3;i++){
        N2[l] = s[i];
        l++;
    }
    
    for(int i = 0;i < length1 + 1;i++){
        int j = 0;
        if(i != length1){
            while(j < length2){
                if(j == 0) S[i][j] = N1[i];
                else if(j == length2 - 1) S[i][j] = N3[i];
                else S[i][j] = ' ';
                j++;
            }
        }else{
            while(j < length2){
                S[i][j] = N2[j];
                j++;
            }
        }
    }
    
    for(int i = 0;i < length1 + 1;i++){
        for(int j = 0;j < length2;j++){
            cout << S[i][j];
        }
        cout << endl;
    }
}


发表于 2019-09-05 07:23:41 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main(){
    char str[85];
    scanf("%s",str);
    int n = strlen(str);
    int n1 = (n+2) / 3,n2 = n+2-2*n1;
    for(int i = 0; i < n1-1; i++){
        printf("%c",str[i]);
        for(int j = 0; j < n2-2; j++) printf(" ");
        printf("%c\n",str[n-i-1]);
    }
    for(int i = n1-1; i < n1+n2-1; i++) printf("%c",str[i]);
    printf("\n");
    return 0;
}

发表于 2019-04-03 11:58:19 回复(0)
#include <iostream>
#include <string>
using namespace std;

//1030 Travel Plan

#define maxn 1e6


int main() {
    int n,n1,n2,n3;
    string str;
    cin>>str;
    n=str.size();
    
    n1=n3=(n+2)/3;
    n2=n+2-n1-n3;
    
    for(int j=0;j<n3-1;++j){
      cout<<  str[j];
      for(int i=0;i<n2-2;++i) cout<<" ";
      cout<<str[n-1-j]<<endl;
    }
    
    //最后一行
    cout<<str[n3-1];
    for(int i=0;i<n2-2;++i){
        cout<<str[n3+i];
    }
    cout<<str[n-n3];
    
    return 0;
}

发表于 2019-02-05 20:33:34 回复(0)
lst = []
n = input()
for i in n:
    lst.append(i)
n1 = (len(lst)+2)//3
n3 = int(n1)
n2 = len(lst)+2-n1-n3
for i in range(0,n1-1):
    print(lst[i]+" "*(n2-2)+lst[len(lst)-i-1] )
for i in range(n1-1,n1+n2-1):
    print(lst[i],end='')

发表于 2018-12-05 15:40:38 回复(0)

//本题用字符串总长度N+2
然后分成3段n1  n3 为( N+2)/3
由于由于n2>=n1=n3
所以n2=(N+2)/3 +余数
本题思路尽量使n1 n3 最大但是又不能大于n2所以只能平分然后剩余部分给n2
最后计算空格
本程序还有改动空间
可以把空格的StringBuffer 改成循环


import java.io.*;
public class Main {

public static void main(String[] args)throws IOException
{
    BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
    String string=reader.readLine();
    StringBuffer temp=new StringBuffer();
    int num=(string.length()+2)/3;
    int len=(string.length()+2)/3+(string.length()+2)%3;
    for(int i=0;i<(string.length()+2)/3+(string.length()+2)%3-2;i++)
    {
        temp.append(" ");
    }
    for(int i=0;i<(string.length()+2)/3-1;i++)
    {
        System.out.println(string.charAt(i)+temp.toString()+string.charAt(string.length()-i-1));
    }
    for(int i=(string.length()+2)/3-1;i<(string.length()+2)/3-1+(string.length()+2)/3+(string.length()+2)%3;i++)
    {
        System.out.print(string.charAt(i));
    }
}
}

发表于 2018-10-06 21:33:24 回复(0)

include <iostream>

include <string>

include <cmath>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main()
{
string str;
cin >> str;
/*
for (int i = 0; i < 85; i++)
{
cout << carray[i];
}
**/
int n1 = 0;
int n2 = 0;
int n3 = 0;
int N = str.length();

n1 = floor((N + 2) / 3);//2
n3 = floor((N + 2) / 3);//2
n2 = N + 2 - n1 - n3;//3

int o[28][28] = {' '};//
for (int i = 0; i < n1; i++)
{
    o[i][0] = str[i];

}

for (int j = 1; j < n2; j++)
{
    o[n1 - 1][j] = str[n1 - 1 + j];
}

for (int k = n1 - 1; k >= 1 && n3 > 1; k--, n3--)
{
    o[k - 1][n2 - 1] = str[N - n3 + 1];
}

for (int i = 0; i < n1; i++)
{
    for (int j = 0; j < n2; j++)
    {
        printf("%c", o[i][j]);
    }
    printf("\n");
}

}
clion通过,oj为什么不通过,感谢各位给与解答

发表于 2018-09-22 20:21:29 回复(0)
s=input()
x=(len(s)+2)//3
y=len(s)+2-2*x
for i in range(x-1):
    l,r=i,-(i+1)
    print(s[l]+' '*(y-2)+s[r])
print(s[x-1:x-1+y])
根据题目公式,需要高低于宽的情况下最长的高,也就是x(高)+1>y(宽),刚好可以用总边长(N+2)除以3下整定x,此时x+1必定大于y,因公式y=N+2-2*x,x+1则y-2
发表于 2018-09-04 17:01:51 回复(0)
思路: 构建二维数组然后输出。
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    string str;
    cin >> str;
    int n1, n2;
    if (str.size() % 3 == 0)
    {
        n1 = str.size() / 3;
        n2 = str.size() - 2 * n1 + 2;
     }
    else
    {
        n1 = str.size() / 3 + 1;
        n2 = str.size() - 2 * n1 + 2;
    }
    vector<vector<char> > v;
    v.resize(n1);
    for (int i = 0; i < n1; i++)
    {
        v[i].resize(n2);
    }
    for (int i = 0; i < n1; i++)
    {
        v[i][0] = str[i];
        v[i][n2 - 1] = str[str.size() - 1 - i];
        if (i == n1 - 1)
        {
            for (int j = 1; j < n2 - 1; j++)
            {
                v[i][j] = str[n1 - 1 + j];
            }
        }
    }
    for (int i = 0; i < n1; i++)
    {
        for (int j = 0; j < n2; j++)
        {
            if (v[i][j] != 0)
            {
                cout << v[i][j];
            }
            else
            {
                cout << " ";
            }
        }
        cout << endl;
    }
    system("pause");
}

发表于 2018-08-25 22:29:29 回复(0)