Description 前几天,老师让良哥做一个画空心三角形的程序来取悦小师妹,可把良哥愁死了,C语言基础学得再扎实,算法不精也是硬伤呀!于是良哥又写好了主程序来找机前的你帮忙了^_^
#include <stdio.h> void holl_triangle(int n); /*函数声明*/ int main() { int n; scanf("%d",&n); /*输入行数n*/ holl_triangle(n); /*调用输入n行的空心三角形的函数*/ return 0; }主程序已给出,请完成holl_triangle函数并提交
Input 输入一个数字 例如 5
Output
* * * * * * * *********输出如上图形
Sample Input 5 Sample Output
* * * * * * * *********参考解答
#include <stdio.h> void holl_triangle(int n); /*函数声明*/ int main() { int n; scanf("%d",&n); /*输入行数n*/ holl_triangle(n); /*调用输入n行的空心三角形的函数*/ return 0; } void holl_triangle(int n) { int i,j; for(j=1; j<=n-1; j++) printf(" "); printf("*\n"); //输出第1行,一个* for(i=2; i<=n-1; i++) { for(j=1; j<=n-i; j++) printf(" "); printf("*"); for(j=1; j<=2*i-3; j++) printf(" "); printf("*\n"); } for(j=1; j<=2*n-1; j++) printf("*"); printf("\n"); }