#include<stdio.h> void main() { int i,j,n; printf("Please input n:\n"); scanf("%d",&n); if(n<0) printf("行数错误!\n"); else { for(j=1;j<=n;j++) { for(i=1;i<=j;i++) { printf("*"); } printf("\n"); } } }
There are two control cycle count variable i, j.
To find the relationship between the j, the relationship between two variables control loop and i,.
Example: Pyramid *
***
*****
******* * two line increases, the number of control lines and the variable j, i and the number of control variables for each line,
Relationship I * = J-2. 1 . Therefore, the inner loop control condition for (i = 1; i < = 2 * j-1; i ++)
if(n<0) printf("Input error!"); else { for(j=1;j<=n;j++) { for(i=1;i<=2*j-1;i++) { printf("*"); } printf("\n"); } }
Another example: inverted triangle *******
*****
***
* Controls the relationship between the number j, and the number of I, 2 * + n-1-2 * J (n-number of input lines of the pyramid)