蛇形矩阵

蛇形矩阵

http://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e

第0行,j=1,每一位数字为:(jj+j)/2
第1行,j=2,每一位数字为:(j
j+j)/2-1.即为,去掉上一行的第一个数,然后后面的数补上来,并且减1

import java.util.Scanner;

/**
 * 蛇形矩阵
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()){
            int N = sc.nextInt();
            snakeNum(N);
        }
    }

    private static void snakeNum(int n){
        if (n == 1) System.out.println(1);
        for (int i = 0; i < n; i++){//有n行需要输出
            for (int j = i+1; j <= n; j++){//每行有多少个数(j个)
                System.out.print(((j*j+j)/2-i)+" ");
            }
            System.out.println();//换行
        }
    }
}
全部评论

相关推荐

码农顶针:估计让你免费辅导老板孩子的学习
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务