题解 | #蛇形矩阵#

蛇形矩阵

http://www.nowcoder.com/practice/f228a074c5274619b26be544962375e1

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n =sc.nextInt();
        int[][] array= new int[n][n];
        int count = 1;
        for (int i=0;i<2*n-2;i++) {
            for (int j=0;j<=i;j++) {
                int x=i-j;
                if (x<n&&j<n) {
                    if(i%2==0) {
                        array[x][j]=count;
                    } else{
                        array[j][x]=count;
                    }
                    count++;
                }
            }
        }
        array[n-1][n-1]=count;
        for (int i=0;i<n;i++) {
            for (int j=0;j<n;j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }
}

全部评论

相关推荐

昨天 20:46
武汉大学 Java
点赞 评论 收藏
分享
10-27 02:29
已编辑
门头沟学院 嵌入式工程师
牛客72783561...:简历不是这么写的,你这两个项目只说了用到了什么技术,却没说取得了什么成果,在我看来这就是你自己做的一个demo,没有价值。你为什么不写你电赛国二的那个项目?
点赞 评论 收藏
分享
12-23 18:51
中南大学 Java
唉又萌混过关:是不是那种收钱盖实习章的机构?
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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