c++打印输出沙漏图形
时间: 2023-11-18 17:51:45 浏览: 93
Dev-C++简单图形输出
Sure, here's an example of how you can print a diamond pattern using asterisks in C:
```C
#include <stdio.h>
void printLine(int spaces, int stars) {
for (int i = 0; i < spaces; i++) {
printf(" ");
}
for (int i = 0; i < stars; i++) {
printf("*");
}
printf("\n");
}
void printDiamond(int n) {
int spaces = n-1;
int stars = 1;
// Print upper half
for (int i = 0; i < n; i++) {
printLine(spaces, stars);
spaces--;
stars += 2;
}
spaces = 1;
stars = n * 2 - 3;
// Print lower half
for (int i = 0; i < n-1; i++) {
printLine(spaces, stars);
spaces++;
stars -= 2;
}
}
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
printDiamond(n);
return 0;
}
```
This program takes the number of rows as input and prints a diamond pattern accordingly. You can adjust the pattern by modifying the `printLine` and `printDiamond` functions.
阅读全文