【字符串处理】1031 Hello World for U (20 分)

1031 Hello World for U (20 分)

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

Code

#include <iostream>
using namespace std;
int main(){
	string s;
	cin>>s;
	int n1, n2, n3;
	int N = s.length();
	int flag = 0;
	for(n2 = 3; n2 <= N; n2++){
		for(n1 = 2; n1 <= (N + 2 - n2) / 2; n1++){
			n3 = n1;
			if(n1 + n2 + n3 - 2 == N && n1 <= n2){
				flag = 1;
				break;
			}
		}
		if(flag)
			break;
	}
	string s1 = s.substr(0, n1);//从0开始取n1个数
	string s2 = s.substr(n1, N - n3 - n1);//从n1开始,取出N - n3 - n1个数
	string s3 = s.substr(N - n3, n3);//从N - n3开始取 n3个数
	for(int i = 0; i < s1.length(); i++){
		cout<<s1[i];
		if(i == s1.length() - 1)
			for(int j = 0; j < s2.length(); j++)
				cout<<s2[j];
		else
			for(int j = 0; j < s2.length(); j++)
				cout<<" ";
		cout<<s3[s1.length() - i - 1];
		cout<<endl;
	}
}

LiuCuo

/**
要求:
1. n1 == n3
2. n2 >= n1
3. n1为在满⾜上述条件的情况下的最⼤值
分析:假设n = 字符串⻓度 + 2,因为2 * n1 + n2 = n,且要保证n2 >= n1, n1尽可能地⼤,分类讨论:
1. 如果n % 3 == 0,n正好被3整除,直接n1 == n2 == n3;
2. 如果n % 3 == 1,因为n2要⽐n1⼤,所以把多出来的那1个给n2
3. 如果n % 3 == 2, 就把多出来的那2个给n2
所以得到公式:n1 = n / 3,n2 = n / 3 + n % 3
把它们存储到⼆维字符数组中,⼀开始初始化字符数组为空格,然后按照u型填充进去,最后输出这个
数组u~~
**/
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
	char c[81], u[30][30];
	memset(u, ' ', sizeof(u));
	scanf("%s", c);
	int n = strlen(c) + 2;
	int n1 = n / 3, n2 = n / 3 + n % 3, index = 0;
	for(int i = 0; i < n1; i++) u[i][0] = c[index++];
	for(int i = 1; i <= n2 - 2; i++) u[n1-1][i] = c[index++];
	for(int i = n1 - 1; i >= 0; i--) u[i][n2-1] = c[index++];
	for(int i = 0; i < n1; i++) {
		for(int j = 0; j < n2; j++)
			printf("%c", u[i][j]);
		printf("\n");
	}
	return 0;
}

Summary

我的就是暴力找规律,大神把规律找出来了,就是把一段字符串分成3份,然后然后除法的结果选择是否将多出来的余数,给到中间字符串中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Amonlisa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值