【错难题】n阶数字正方形/n阶数字三角形/n阶递减三角形

来源:互联网 发布:mui.js 帮助文档 编辑:程序博客网 时间:2024/05/29 02:39

终于写出来~慢慢敲敲改改,需要多练些嵌套循环~

n阶数字正方形

#include <stdio.h>int main(){    int n;    int i, j;     int c; /* i value and number of row and column, j value in row */    scanf("%d", &n);    for (i = 1; i <= n; i++)    {        printf("%d", i);        for (j = i,c = 1; c < n; c++)        {            if (c <= n-1)            {                printf(" ");            }            printf("%d",j);        }        if (i <= n-1)        {            printf("\n");        }    }    return 0;}            

n阶数字三角形

#include <stdio.h>int main() {    int n;    int i; /* value and number of row */    int j;/* value of row */    int c;/* number of column*/    scanf("%d", &n);    for (i = 1; i <= n; i++)    {        printf("%d", i);        for (j = i, c = n - i; c > 0; c--)        {            if (i < n)            {                printf(" ");            }                        printf("%d", j);                              }        if (i < n)            {                printf("\n");            }    }    return 0;}


n阶递减三角形

#include <stdio.h>int main() {    int n;    int i; /* value and number of row*/    int j;    scanf("%d", &n);    for (i = n; i > 0; i--)    {        printf("%d", i);        if (i > 1)        {            printf(" ");        }        for (j = i-1; j > 0; j--)        {            printf("%d", j);            if (j > 1)            {                printf(" ");            }        }        if (j = 1)        {            printf("\n");        }    }     return 0;}




原创粉丝点击