输出倒三角图案
输出如下图形:
* * * * * * * * * * * * * * * * * * * * *
C语言代码
//打印倒三角图案 #include <stdio.h> void reversed_triangle(int n); //函数声明 int main() { int n; puts("需要输出几层倒三角: "); scanf("%d",&n); reversed_triangle(n); return 0; } void reversed_triangle(int n) { for (int i = 0; i < n; i++) { for (int k = 0; k < i; k++) { printf(" "); } for (int j = 0; j < n - i; j++) { printf("* "); } printf("\n"); } }
Python代码:
# 打印倒三角图案 def reversed_triangle(n): for i in range(0, n): print(" " * i, end='') for j in range(0, n - i): print("* ", end='') print() n = input("需要输出几层倒三角: ") reversed_triangle(int(n))