//土尔逊Torson 在2023/2/5 日 编写 #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <string> using namespace std; int main() { char str[80] = { 0 }; char pic[80][80] = { 0 }; while (scanf("%s", &str) != EOF) { int size = 0; // 计算每条字符串的长度 for (int i = 0; i < 80; ++i) { if (str[i] != 0) { size++; } else { break; } } int n = (size + 2) / 3; //对字符串长度除以3,取下整数 (就是U型的左垂直在字符中的前N个字符数) int m = (size + 2) % 3; //对字符串长度取3的余数 int bottom = n + m; //是U型的底边数 (从字符串第 N+1 字符起 bottom 个在字符串中的字符数) int x = 0, y = 0; //U型图样字符坐标 //绘制U型图 //初始化U型图底层(第一层)为空格 ' '字符 for (int i = 0; i < bottom; ++i) { for (int j = 0; j < bottom; ++j) { pic[i][j] = ' '; } } //绘制第二层就是U型图形 //绘制左垂直线 for (int i = x, j = y, k = 0; i < n - 1; ++i, ++k) { pic[i][j] = str[k]; } //绘制底边 for (int i = x + n - 1, j = y, k = n - 1 ; j < bottom; ++j, ++k) { pic[i][j] = str[k]; } //绘制右垂直线 for (int i = x + n - 2, j = y + bottom - 1, k = n - 1 + bottom; i >= 0; --i, ++k) { pic[i][j] = str[k]; } //绘制最终图像 for (int i = 0; i < bottom; ++i) { printf("%s\n", pic[i]); } //printf("str=%s size=%d n=%d m=%d\n", str, size, n, m); // 调试函数 // 每次处理完一串字符就进行一次初始化为 0 for (int i = 0; i < 80; ++i) { str[i] = 0; for (int j = 0; j < 80; ++j) { pic[i][j] = 0; } } } //system("pause"); return EXIT_SUCCESS; }