题解 | #蛇形矩阵#

蛇形矩阵

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int s = in.nextInt();
        int a = 1;
        for (int i = 0; i < s; i++) {
            //先考虑列
            a = a+i;
            int b = a;
            for (int j = i; j < s; j++) {
                if (j==i){
                    System.out.print(b+" ");
                    continue;
                }
                b += j+1;
                if (j==s-1){
                    System.out.println(b);
                    continue;
                }
                System.out.print(b+" ");
            }
        }
    }
}

全部评论

相关推荐

牛客383479252号:9,2学生暑期实习失利开始投小厂,给这群人整自信了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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