7-57 又来一个上三角数字三角形

7-57 又来一个上三角数字三角形,第1张

输入一个正整数n,输出具有n层的上三角数字三角形。

输入格式:

只有一个正整数n,1<=n<=100。

输出格式:

一个上三角数字三角形,每个数字占四个字符位置。

输入样例:
5
输出样例:
   1   6  10  13  15
   2   7  11  14
   3   8  12
   4   9
   5

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i=1;i<=n;i++) {
            int k=i;
            int h=n;
            for (int j=i;j<=n;j++) {
                System.out.printf("%4d",k);
                k=k+h;
                h--;
            }
            System.out.println("");
        }
    }
}

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/799573.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存