如何在c中打印类似以下格式的数字?

时间:2019-11-24 16:40:12

标签: c logic-programming

如何像这样打印 See This

2 个答案:

答案 0 :(得分:1)

#include <stdio.h>

typedef enum 
{
    O_VER,
    O_HOR,
    /* you can add your own */
}ORDER_t;

void printInCols(int startnum, int nCols, int nRows, ORDER_t order)
{
    for(int row = 0; row < nRows; row++)
    {
        for(int col = 0; col < nCols; col++)
        {
            switch(order)
            {
                case O_VER:
                    printf("%6d\t", startnum + row + col * (nCols + 1));
                    break;
                case O_HOR:
                    printf("%6d\t", startnum + col + row * (nCols));
                    break;
            }
        }
        printf("\n");
    }
}

int main()
{
    printInCols(4,6,7,O_HOR);
    printf("\n\n");
    printInCols(4,6,7,O_VER);
}

https://godbolt.org/z/WAj8Es

答案 1 :(得分:1)

#include <stdio.h>
int main(void) {
    for(int i=1; i<=10; i++) printf("%i\t%i\r\n", i, i+10);
    return 0;
}

输出:

1       11
2       12
3       13
4       14
5       15
6       16
7       17
8       18
9       19
10      20