原文:

给定一个数字 n ,然后以数字形式打印数字。

示例:

input : 5
output : 
  - - 
|    
 - - 
    |
 - - 
input :  8 
output :
 - - 
|   |
 - - 
|   |
 - -

说明: 取一个 5*5 大小的矩阵,在矩阵中存储 0 和 1。如果矩阵单元为 0,则用于空格,如果矩阵单元为 1,则用于水平线或垂直线。

如果行号为偶数,则打印水平的 (-) 行,如果行号为奇数,则打印垂直的 ( | ) 行。

c

//  c   program to print
// number in digital form
#include 
#include 
using namespace std;
// function to print numbers
void print(int mat[][5])
{
    // if in matrix row number is even then print "-"
    // otherwise print "|"
    for (int i = 0; i < 5; i  ) {
        for (int j = 0; j < 5; j  ) {
            if (i % 2 == 0) {
                if (mat[i][j] == 1)
                    cout << "-";
                else
                    cout << " ";
            }
            else {
                if (mat[i][j] == 1)
                    cout << "|";
                else
                    cout << " ";
            }
        }
        cout << endl;
    }
}
void digit0()
{
    // in matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 0, 0, 0, 0 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 } };
    print(mat);
}
void digit1()
{
    // in matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 0, 0, 0, 0 },
                    { 0, 0, 1, 0, 0 },
                    { 0, 0, 0, 0, 0 },
                    { 0, 0, 1, 0, 0 },
                    { 0, 0, 0, 0, 0 } };
    print(mat);
}
void digit2()
{
    // in matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 0 },
                    { 0, 1, 0, 1, 0 } };
    print(mat);
}
void digit3()
{
    // in matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 } };
    print(mat);
}
void digit4()
{
    // in matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 0, 0, 0, 0 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 0, 0, 0, 0 } };
    print(mat);
}
void digit5()
{
    // in matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 0 },
                    { 0, 1, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 } };
    print(mat);
}
void digit6()
{
    // in matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 0 },
                    { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 } };
    print(mat);
}
void digit7()
{
    // in matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 0, 0, 0, 0 } };
    print(mat);
}
void digit8()
{
    // in matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 } };
    print(mat);
}
void digit9()
{
    // in matrix 0 used for space
    // and 1 for either - or |
    int mat[5][5] = { { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 },
                    { 0, 1, 0, 1, 0 } };
    print(mat);
}
// function to check number
void checkdigit(int num)
{
    // for digit 0
    if (num == 0)
        digit0();
    // for digit 1
    else if (num == 1)
        digit1();
    // for digit 2
    else if (num == 2)
        digit2();
    // for digit 3
    else if (num == 3)
        digit3();
    // for digit 4
    else if (num == 4)
        digit4();
    // for digit 5
    else if (num == 5)
        digit5();
    // for digit 6
    else if (num == 6)
        digit6();
    // for digit 7
    else if (num == 7)
        digit7();
    // for digit 8
    else if (num == 8)
        digit8();
    // for digit 9
    else if (num == 9)
        digit9();
}
// driver program
int main()
{
    // input a number
    int num = 9;
    // function call to check digit
    checkdigit(num);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print
// number in digital form
import java.io.*;
class gfg {
    // function to print numbers
    static void print(int mat[][])
    {
        // if in matrix row number is even then print "-"
        // otherwise print "|"
        for (int i = 0; i < 5; i  ) {
            for (int j = 0; j < 5; j  ) {
                if (i % 2 == 0) {
                    if (mat[i][j] == 1)
                        system.out.print("-");
                    else
                        system.out.print(" ");
                }
                else {
                    if (mat[i][j] == 1)
                        system.out.print("|");
                    else
                        system.out.print(" ");
                }
            }
            system.out.println();
        }
    }
    static void digit0()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit1()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 0, 0, 0, 0 },
                        { 0, 0, 1, 0, 0 },
                        { 0, 0, 0, 0, 0 },
                        { 0, 0, 1, 0, 0 },
                        { 0, 0, 0, 0, 0 } };
        print(mat);
    }
    static void digit2()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 0 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit3()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit4()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 0, 0, 0, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 } };
        print(mat);
    }
    static void digit5()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 0 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit6()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 0 },
                        { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit7()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 } };
        print(mat);
    }
    static void digit8()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit9()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int mat[][] = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    // function to check number
    static void checkdigit(int num)
    {
        // for digit 0
        if (num == 0)
            digit0();
        // for digit 1
        else if (num == 1)
            digit1();
        // for digit 2
        else if (num == 2)
            digit2();
        // for digit 3
        else if (num == 3)
            digit3();
        // for digit 4
        else if (num == 4)
            digit4();
        // for digit 5
        else if (num == 5)
            digit5();
        // for digit 6
        else if (num == 6)
            digit6();
        // for digit 7
        else if (num == 7)
            digit7();
        // for digit 8
        else if (num == 8)
            digit8();
        // for digit 9
        else if (num == 9)
            digit9();
    }
    // driver program
    public static void main (string[] args)
    {
        // input a number
        int num = 9;
        // function call to check digit
        checkdigit(num);
    }
}
// this code is contributed by vt_m.

python 3

# python3 program to prints
# number in digital form
# function to prints numbers
def prints(mat):
    # if in matrix row number is even then
    # prints "-" otherwise prints "|"
    for i in range(5):
        for j in range(5):
            if (i % 2 == 0):
                if (mat[i][j] == 1):
                    print('', end = '-')
                else:
                    print('', end = ' ')
            else:
                if (mat[i][j] == 1):
                    print('', end = '|')
                else:
                    print('', end = ' ')
        print()
def digit0():
    # in matrix 0 used for space
    # and 1 for either - or |
    mat = [ [ 0, 1, 0, 1, 0 ],
            [ 1, 0, 0, 0, 1 ],
            [ 0, 0, 0, 0, 0 ],
            [ 1, 0, 0, 0, 1 ],
            [ 0, 1, 0, 1, 0 ] ]
    prints(mat)
def digit1():
    # in matrix 0 used for space
    # and 1 for either - or |
    mat = [ [ 0, 0, 0, 0, 0 ],
            [ 0, 0, 1, 0, 0 ],
            [ 0, 0, 0, 0, 0 ],
            [ 0, 0, 1, 0, 0 ],
            [ 0, 0, 0, 0, 0 ] ]
    prints(mat)
def digit2():
    # in matrix 0 used for space
    # and 1 for either - or |
    mat = [ [ 0, 1, 0, 1, 0 ],
            [ 0, 0, 0, 0, 1 ],
            [ 0, 1, 0, 1, 0 ],
            [ 1, 0, 0, 0, 0 ],
            [ 0, 1, 0, 1, 0 ] ]
    prints(mat)
def digit3():
    # in matrix 0 used for space
    # and 1 for either - or |
    mat = [ [ 0, 1, 0, 1, 0 ],
            [ 0, 0, 0, 0, 1 ],
            [ 0, 1, 0, 1, 0 ],
            [ 0, 0, 0, 0, 1 ],
            [ 0, 1, 0, 1, 0 ] ]
    prints(mat)
def digit4():
    # in matrix 0 used for space
    # and 1 for either - or |
    mat = [ [ 0, 0, 0, 0, 0 ],
            [ 1, 0, 0, 0, 1 ],
            [ 0, 1, 0, 1, 0 ],
            [ 0, 0, 0, 0, 1 ],
            [ 0, 0, 0, 0, 0 ] ]
    prints(mat)
def digit5():
    # in matrix 0 used for space
    # and 1 for either - or |
    mat = [ [ 0, 1, 0, 1, 0 ],
            [ 1, 0, 0, 0, 0 ],
            [ 0, 1, 0, 1, 0 ],
            [ 0, 0, 0, 0, 1 ],
            [ 0, 1, 0, 1, 0 ] ]
    prints(mat)
def digit6():
    # in matrix 0 used for space
    # and 1 for either - or |
    mat = [ [ 0, 1, 0, 1, 0 ],
            [ 1, 0, 0, 0, 0 ],
            [ 0, 1, 0, 1, 0 ],
            [ 1, 0, 0, 0, 1 ],
            [ 0, 1, 0, 1, 0 ] ]
    prints(mat)
def digit7():
    # in matrix 0 used for space
    # and 1 for either - or |
    mat = [ [ 0, 1, 0, 1, 0 ],
            [ 0, 0, 0, 0, 1 ],
            [ 0, 0, 0, 0, 0 ],
            [ 0, 0, 0, 0, 1 ],
            [ 0, 0, 0, 0, 0 ] ]
    prints(mat)
def digit8():
    # in matrix 0 used for space
    # and 1 for either - or |
    mat = [ [ 0, 1, 0, 1, 0 ],
            [ 1, 0, 0, 0, 1 ],
            [ 0, 1, 0, 1, 0 ],
            [ 1, 0, 0, 0, 1 ],
            [ 0, 1, 0, 1, 0 ] ]
    prints(mat)
def digit9():
    # in matrix 0 used for space
    # and 1 for either - or |
    mat = [ [ 0, 1, 0, 1, 0 ],
            [ 1, 0, 0, 0, 1 ],
            [ 0, 1, 0, 1, 0 ],
            [ 0, 0, 0, 0, 1 ],
            [ 0, 1, 0, 1, 0 ] ]
    prints(mat)
# function to check number
def checkdigit(num):
    # for digit 0
    if (num == 0):
        digit0()
    # for digit 1
    elif (num == 1):
        digit1()
    # for digit 2
    elif (num == 2):
        digit2()
    # for digit 3
    elif (num == 3):
        digit3()
    # for digit 4
    elif (num == 4):
        digit4()
    # for digit 5
    elif (num == 5):
        digit5()
    # for digit 6
    elif (num == 6):
        digit6()
    # for digit 7
    elif (num == 7):
        digit7()
    # for digit 8
    elif (num == 8):
        digit8()
    # for digit 9
    elif (num == 9):
        digit9()
# driver code
if __name__=='__main__':
    # input a number
    num = 9
    # function call to check digit
    checkdigit(num)
# this code is contributed by rutvik_56

c

// c# program to print
// number in digital form
using system;
class gfg {
    // function to print numbers
    static void print(int [,]mat)
    {
        // if in matrix row number is even
        // then print "-" otherwise print "|"
        for (int i = 0; i < 5; i  ) {
            for (int j = 0; j < 5; j  ) {
                if (i % 2 == 0) {
                    if (mat[i,j] == 1)
                        console.write("-");
                    else
                        console.write(" ");
                }
                else {
                    if (mat[i,j] == 1)
                        console.write("|");
                    else
                        console.write(" ");
                }
            }
            console.writeline();
        }
    }
    static void digit0()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit1()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 0, 0, 0, 0 },
                        { 0, 0, 1, 0, 0 },
                        { 0, 0, 0, 0, 0 },
                        { 0, 0, 1, 0, 0 },
                        { 0, 0, 0, 0, 0 } };
        print(mat);
    }
    static void digit2()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 0 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit3()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit4()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 0, 0, 0, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 } };
        print(mat);
    }
    static void digit5()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 0 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit6()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 0 },
                        { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit7()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 0, 0, 0, 0 } };
        print(mat);
    }
    static void digit8()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    static void digit9()
    {
        // in matrix 0 used for space
        // and 1 for either - or |
        int [ ,]mat = { { 0, 1, 0, 1, 0 },
                        { 1, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 },
                        { 0, 0, 0, 0, 1 },
                        { 0, 1, 0, 1, 0 } };
        print(mat);
    }
    // function to check number
    static void checkdigit(int num)
    {
        // for digit 0
        if (num == 0)
            digit0();
        // for digit 1
        else if (num == 1)
            digit1();
        // for digit 2
        else if (num == 2)
            digit2();
        // for digit 3
        else if (num == 3)
            digit3();
        // for digit 4
        else if (num == 4)
            digit4();
        // for digit 5
        else if (num == 5)
            digit5();
        // for digit 6
        else if (num == 6)
            digit6();
        // for digit 7
        else if (num == 7)
            digit7();
        // for digit 8
        else if (num == 8)
            digit8();
        // for digit 9
        else if (num == 9)
            digit9();
    }
    // driver program
    public static void main ()
    {
        // input a number
        int num = 9;
        // function call to check digit
        checkdigit(num);
    }
}
// this code is contributed by vt_m.

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


output: 

 - - 
|   |
 - - 
    |
 - -