目录
1031 Hello World for U (20分)
大佬的地址:刷PTA可以学习的大佬的地址
https://blog.csdn.net/liuchuo 婼神的地址
https://www.mmuaa.com/post/category/pat 斐斐のBlog
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
生词
vertical 垂直的
left to right along the bottom 从 左到右到底部
题目大意
给你一个字符串,要求输出一个U字形 底部的长度要不低于(可以等于)边部
分析(我的脑瘫分析)
① 无脑写两个循环,求出最长的边
② 根据边算出底部,循环输出
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(void) {
char Array[81];
int n1=0, n2, N,i;
scanf("%s", &Array);
N = strlen(Array);
for (i = 1; i < N; i++) {
for (int j = 1; 2 * i + j <= 2 + N; j++) {
if (j >= i) {
if (i >n1) {
n1 = i;
n2 = j;
}
}
}
}
for (i = 0; i < n1-1; i++) {
printf("%c", Array[i]);
for (int j = 0; j < N-2*n1; j++) {
printf(" ");
}
printf("%c\n", Array[N-i-1]);
}
int T=N-i-1;
for (; i <=T; i++) {
printf("%c", Array[i]);
}
}
下面是看了大佬的博客之后发现的
下面贴一个 斐斐のBlog(她博客就叫这个)---侵权删 的代码
大佬的简洁的代码
#include <iostream>
#include <string>
using namespace std;
int main(){
string input;
cin >> input;
int len = input.length();
int side = (len + 2) / 3;
int buttom = len + 2 - (2 * side);
for(int i = 0; i < side - 1; i++){
cout << input[i];
for(int j = 0; j < buttom - 2; j++) cout << ' ';
cout << input[len - 1 - i];
cout << endl;
}
for(int i = side - 1; i < side + buttom - 1; i++)
cout << input[i];
cout << endl;
return 0;
}