题目描述
由题可知
$n = 2*n1 + n2$
$n1 = n3$
$n2 >= n1$
分析
$(n + 2) \% 3 = 0, n2 = n1$
$(n + 2) \% 3 = 1, n2 = n1 + 1$
$(n + 2) \% 3 = 2, n2 = n1 + 2$
所以可得
$n1 = (n + 2) / 3$
$n2 = n1 + (n + 2) \% 3$
C++ 代码
#include <iostream>
#include <string>
using namespace std;
const int MAX_N = 81;
char g[MAX_N][MAX_N];
int main() {
string str;
cin >> str;
int n = str.length();
int n1 = (n + 2) / 3;
int n3 = n1;
int n2 = n1 + (n + 2) % 3;
int cnt = 0;
for (int i = 0; i < n1; i++) g[i][0] = str[cnt++];
for (int i = 1; i < n2; i++) g[n1 - 1][i] = str[cnt++];
for (int i = n3 - 2; i >= 0; i--) g[i][n2 - 1] = str[cnt++];
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
if (g[i][j]) printf("%c", g[i][j]);
else printf(" ");
}
printf("\n");
}
return 0;
}