Skip to content

Latest commit

 

History

History
173 lines (150 loc) · 3.55 KB

0012.打印数字图形.md

File metadata and controls

173 lines (150 loc) · 3.55 KB

12. 打印数字图形

题目链接

C++

#include<iostream>
#include<vector>
using namespace std;

void printTopPart(int n) {
    for (int i = 1; i <= n; i++) {
        // 打印空格
        for (int j = 1; j <= n - i; ++j) {
            cout << " ";
        }

        // 打印递增数字
        for (int j = 1; j <= i; j++) {
            cout << j;
        }

        // 打印递减数字
        for (int j = i - 1; j >= 1; j--) {
            cout << j;
        }
        cout << endl;
    }
}

void printBottomPart(int n) {
    for (int i = n - 1; i >= 1; i--) {
        // 打印空格
        for (int j = 1; j <= n - i; j++) {
            cout << " ";
        }
        // 打印递增数字
        for (int j = 1; j <= i; j++) {
            cout << j;
        }

        // 打印递减数字
        for (int j = i - 1; j >= 1; j--) {
            std::cout << j;
        }
        cout << endl;
    }
}
int main() {
    int n;
    while (cin >> n) {
        if (n < 1 || n > 9) {
            cout << "输入的整数n超出范围" << endl;
        }
        printTopPart(n);
        printBottomPart(n);
    }
}

Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            for (int i = 1; i <= n; i++) {
                print(n - i, i);
            }
            for (int i = n - 1; i >= 1; i--) {
                print(n - i, i);
            }
        }
    }

    public static void print(int blank, int n) {
        //  前面需要补齐空格
        for (int i = 0; i < blank; i++) {
            System.out.print(" ");
        }
        for (int i = 1; i <= n; i++) {
            System.out.print(i);
        }
        for (int i = n - 1; i > 0; i--) {
            System.out.print(i);
        }
        System.out.println();
    }
}

python

while True:
    try:
        n = int(input())
        for i in range(1, n + 1):
            print(' ' * (n - i), end='')
            print(''.join(map(str, range(1, i + 1))) + ''.join(map(str, range(i - 1, 0, -1))))
        for i in range(n - 1, 0, -1):
            print(' ' * (n - i), end='')
            print(''.join(map(str, range(1, i + 1))) + ''.join(map(str, range(i - 1, 0, -1))))
    except:
        break

Go

Js

C

#include <stdio.h>

// 打印上半部分的三角形 
void printTopPart(int n) {
    for (int i = 1; i <= n; i++) {
        // 打印空格
        for (int j = 1; j <= n - i; ++j) {
            printf(" ");
        }

        // 打印递增数字
        for (int j = 1; j <= i; j++) {
            printf("%d", j);
        }

        // 打印递减数字
        for (int j = i - 1; j >= 1; j--) {
            printf("%d", j);
        }
        printf("\n");
    }
}

// 打印下半部分的三角形 
void printBottomPart(int n) {
    for (int i = n - 1; i >= 1; i--) {
        // 打印空格
        for (int j = 1; j <= n - i; j++) {
            printf(" ");
        }

        // 打印递增数字
        for (int j = 1; j <= i; j++) {
            printf("%d", j);
        }

        // 打印递减数字
        for (int j = i - 1; j >= 1; j--) {
            printf("%d", j);
        }
        printf("\n");
    }
}

int main() {
    int n;
    while (scanf("%d", &n) == 1) {
        if (n < 1 || n > 9) {
            printf("n 值不合法\n");
        }
        printTopPart(n);
        printBottomPart(n);
    }
    return 0;
}