思路:
對於U形,我們把左邊、底邊、右邊設爲n1,n2,n3。按照題目所給的規律,即n1 = n3 = 小於n2的最大的數,且n1 + n2 + n3 = N + 2(因爲兩個角落有重疊)
在草稿紙上畫了幾個特殊情況(N=5畫到了N=10)可以發現都是先增加底邊的字符數,直到n1 = n2 = n3後纔開始增加側邊的字符數
可以算出n1 = n3 = (N + 2)/ 3,向下取整
n2 = N + 2 - n1 - n3
直接輸出就可以了,注意控制一下空格之類的
代碼:
#include <cstdio>
#include <string.h>
char str[100];
int main(){
scanf("%s",str);
int N = strlen(str);
int n1,n2,n3;
n1 = n3 = (N + 2)/3;
n2 = N + 2 - n1 - n3;
for (int i = 0; i < n1 - 1; i ++){
printf("%c",str[i]);
for (int j = 0; j < n2 - 2; j ++){
printf(" ");
}
printf("%c\n",str[N - i - 1]);
}
for (int i = 0; i < n2; i ++){
printf("%c",str[n1 + i - 1]);
}
return 0;
}