描述
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!
> 输出:
h !
e d
l l
lowor
w m
w o
. .
n r
owcode
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main(){
char a[80];
while(scanf("%s", &a) != EOF){ //当不知道要输入多少次时用while,当知道循环次数时最好用for循环
char marx[80][80];
for(int i = 0; i < 80; i++){ //初始化数组
for(int j = 0; j < 80; j++){
marx[i][j] = ' ';
}
}
int n = 0, k = 0 ,n1, n2; //此时的n1、n2代表数组中的下标,并不是长度,因为数组的下标从零开始
while(a[n] != '\0') { //计算出字符串的长度
n++;
}
for(int i = 0; i < (n+2)/3; i++){ //给U型左边进行赋值
marx[i][0] = a[k++];
n1 = i;
}
for(int j = 1; j < (n+2)/3 + (n+2)%3 ; j++){ //给U型底进行赋值
marx[n1][j] = a[k++];
n2 = j;
}
for(int t = n1 - 1; t >= 0; t--){ //给U型右边进行赋值
marx[t][n2] = a[k++];
}
for(int i = 0; i <= n1; i++){ //依次输出U型
for(int j = 0; j <= n2; j++){
printf("%c", marx[i][j]);
}
cout << endl;
}
}
return 0;
}