题解 | #蛇形矩阵#

蛇形矩阵

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

找规律,每一行都是 base + step。

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int input = sc.nextInt();
            StringBuilder builder = new StringBuilder();
            int base = 1;
            for (int i = 0; i < input; i++) {
                base += i;
                builder.setLength(0);
                builder.append(base);
                int prev = base;
                for (int step = i + 2; step <= input; step++) {
                    int current = prev + step;
                    prev = current;
                    builder.append(" " + current);
                }
                System.out.println(builder);
            }
        }
    }
}
全部评论

相关推荐

面试摇了我吧:啊哈哈面试提前五个小时发,点击不能参加就是放弃
点赞 评论 收藏
分享
评论
5
1
分享
牛客网
牛客企业服务