打印出三星堆
先尝试逐层打印
for (int i = 0 ; i < 10; i++) { printf(" "); }
for (int i = 0 ; i < 1; i++) { printf("* "); }
printf("\n");
for (int i = 0 ; i < 9; i++) { printf(" "); }
for (int i = 0 ; i < 2; i++) { printf("* "); }
printf("\n");
for (int i = 0 ; i < 8; i++) { printf(" "); }
for (int i = 0 ; i < 3; i++) { printf("* "); }
printf("\n");
使用循环打印,因为发现层数与空格/星号之间的关系是:随着层数的变大 空格变少 星号变多
// i = 0 1 2 3 4
// 9 8 7 6 5 空格k = 9 - i
// 1 2 3 4 5 星号a = i + 1
for (int i = 0 ; i < 5 ; i++) {
for (int k = 0 ; k < 9-i; k++) {printf(" ");}
for (int a = 0; a < i+1; a++) {printf("* ");}
printf("\n");
}
打印出字母三角形
// 字符 'A' + 0 1 2 3 4 = ASCII的 ---> ABCDE
for (int a = 0; a < 5; a++) {
for (int k = 0 ; k < a + 3; k++) {printf(" ");}
for (int i = 0; i < 5 - a; i++) {printf("%c", (char)('A' + i));}
for (int i = 3 - a; i >= 0; i--) {printf("%c", (char)('A' + i));}
printf("\n");
}
printf("\n");
printf("\n");
for (int a = 0; a < 5; a++) {
for (int k = 0 ; k < 7 - a; k++) {printf(" ");}
for (int i = 0; i < a + 1; i++) {printf("%c", (char)('A' + i));}
for (int i = a - 1; i >= 0; i--) {printf("%c", (char)('A' + i));}
printf("\n");
}