编程打印空心字符菱形空心三角形
时间: 2023-06-09 12:06:13 浏览: 74
好的,您可以使用以下代码来打印出空心字符的菱形和空心三角形:
空心字符菱形:
```
#include <stdio.h>
void printHollowDiamond(int n) {
int i, j, space = 1;
space = n - 1;
for (j = 1; j <= n; j++) {
for (i = 1; i <= space; i++) {
printf(" ");
}
printf("*");
for (i = 1; i < 2*(j - 1); i++) {
printf(" ");
}
if (j != 1) {
printf("*");
}
printf("\n");
space--;
}
space = 1;
for (j = 1; j <= n - 1; j++) {
for (i = 1; i <= space; i++) {
printf(" ");
}
printf("*");
for (i = 1; i < 2*(n - j - 1); i++) {
printf(" ");
}
if (j != n - 1) {
printf("*");
}
printf("\n");
space++;
}
}
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
printHollowDiamond(n);
return 0;
}
```
空心字符三角形:
```
#include <stdio.h>
void printHollowTriangle(int n) {
int i, j, space = n - 1;
for (j = 1; j <= n; j++) {
for (i = 1; i <= space; i++) {
printf(" ");
}
printf("*");
for (i = 1; i < 2*(j - 1); i++) {
if (j == n || i == 1 || i == 2*(j - 2)) {
printf("*");
} else {
printf(" ");
}
}
if (j != 1) {
printf("*");
}
printf("\n");
space--;
}
}
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
printHollowTriangle(n);
return 0;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)