题解 | #Hello World for U#
Hello World for U
https://www.nowcoder.com/practice/c6e414fddd7c401887c350c9cc41f01b
#include "cstdio"
#include "string"
using namespace std;
// 大家用笔和纸画一下就知道了
// 因为 x <= y; 且 2x + y = N - 2
// n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N }
// 因为x<=y 不妨取x的最大值 即 x=y 得下结果
// int n3 = (N + 2) / 3, n1 = n3, n2 = N - (n1 + n3);
// 用笔纸画一下就知道 因为这里的 / 是向下取整 所以n1 = n3 ,
// (接上)所以得到的n1 n3 是包含了 两个竖边与底边重叠多算的那两个字符
// 故而 n2 = N - 2*n1 n2 这里的n2 就是不包含重叠的 字符数量
int main() {
char buf[1024];
while (~scanf("%s", buf)) {
string input = buf;
int N = input.length();
int n3 = (N + 2) / 3, n1 = n3, n2 = N - (n1 + n3);
// 图案打印 申请二维数组
int L = n1, M = n2 + 2;
char matrix[L][M];
// printf("n1=%d n2=%d n3=%d\n", n1, n2, n3);
for (int i = 0; i < L; ++i) {
for (int j = 0; j < M; ++j) {
matrix[i][j] = ' ';
}
}
/*
a e
bcd
*/
int index = 0;
// 填充你n1
for (int i = 0; i < L; ++i) {
matrix[i][0] = input[index++];
}
// 填充你n2
for (int i = 1; i < M - 1; ++i) {
matrix[n1 - 1][i] = input[index++];
}
// 填充你n3
for (int i = L - 1; i >= 0; --i) {
matrix[i][M - 1] = input[index++];
}
// printf("index=%d\n", index);
for (int i = 0; i < L; ++i) {
for (int j = 0; j < M; ++j) {
printf("%c", matrix[i][j]);
}
printf("\n");
}
}
return 0;
}

