题解 | #蛇形矩阵#

蛇形矩阵

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


public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            // 输出值
            int y = 1;  //列起始值
            int yCount = 1;  //列间隔起始值
            // 外层控制行,内层控制列
            for(int i = 1;i <= n;i++){
                int x = y; //行起始值
                int xCount = i + 1; //行间隔起始值
                for(int j = 1 ;j <= n-i+1;j++){ //列循环次数
                    if(j == n-i+1){
                        System.out.println(x);
                    }else{
                        System.out.print(x + " ");
                    }
                    x = x + xCount;
                    xCount++;
                }
                y = y + yCount;
                yCount++;
            }
        }
    }
}
全部评论

相关推荐

下个早班:秒挂就是不缺人
点赞 评论 收藏
分享
程序员饺子:正常 我沟通了200多个 15个要简历 面试2个 全投的成都的小厂。很多看我是27直接不会了😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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