第三次CCF计算机软件能力认证考试题解(Java)--201412--Z字形扫描--100分通过

问题描述
  在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan)。给定一个n×n的矩阵,Z字形扫描的过程如下图所示:

  对于下面的4×4的矩阵,
  1 5 3 9
  3 7 5 6
  9 4 6 4
  7 3 1 3
  对其进行Z字形扫描后得到长度为16的序列:
  1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3
  请实现一个Z字形扫描的程序,给定一个n×n的矩阵,输出对这个矩阵进行Z字形扫描的结果。
输入格式
  输入的第一行包含一个整数n,表示矩阵的大小。
  输入的第二行到第n+1行每行包含n个正整数,由空格分隔,表示给定的矩阵。
输出格式
  输出一行,包含n×n个整数,由空格分隔,表示输入的矩阵经过Z字形扫描后的结果。
样例输入
4
1 5 3 9
3 7 5 6
9 4 6 4
7 3 1 3
样例输出
1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3
评测用例规模与约定
  1≤n≤500,矩阵元素为不超过1000的正整数。
完整代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n;
        n=sc.nextInt();
        int[][] matrix=new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j]=sc.nextInt();
            }
        }
        
        for (int i = 0; i < n; i++) {
            int row=0;
            int column=0;
            if (i%2==0) {
                row=i;
                column=0;
                System.out.print(matrix[row][column]+" ");
                while (row-1>=0&&column+1<n) {
                    row--;
                    column++;
                    System.out.print(matrix[row][column]+" ");
                }
            } else {
                row=0;
                column=i;
                System.out.print(matrix[row][column]+" ");
                while (row+1<n&&column-1>=0) { 
                    row++;
                    column--;
                    System.out.print(matrix[row][column]+" "); 
                }
            }
        }
        for (int i = 1; i <n ; i++) {
            int row=0;
            int column=0;
            if(n%2!=0){
                if (i%2==0) {
                row=n-1;
                column=i;
                System.out.print(matrix[row][column]+" ");
                while (row-1>=0&&column+1<n) { 
                    row--;
                    column++;
                    System.out.print(matrix[row][column]+" "); 
                    }
                } else {
                    row=i;
                    column=n-1;
                    System.out.print(matrix[row][column]+" ");
                    while (row+1<n&&column-1>=0) {
                        row++;
                        column--;
                        System.out.print(matrix[row][column]+" ");
                    }
                }
            } else {
                if (i%2==0) {
                    row=i;
                    column=n-1;
                    System.out.print(matrix[row][column]+" ");
                    while (row+1<n&&column-1>=0) {
                        row++;
                        column--;
                        System.out.print(matrix[row][column]+" ");
                    }
                
                } else {
                    row=n-1;
                    column=i;
                    System.out.print(matrix[row][column]+" ");
                    while (row-1>=0&&column+1<n) { 
                        row--;
                        column++;
                        System.out.print(matrix[row][column]+" "); 
                    }
                }
            }
            
        }
    }

}
注意事项

该代码已在CCF认证平台上满分100分通过,将代码复制到CCF认证平台上进行测试时,注意其提交要求,我的程序没有使用package语句来定义包的信息。(如果定义了将无法评测),所以需要将以上代码的package语句删除,注意扫描输出矩阵对角线的后半部分时矩阵的大小n要分奇数、偶数两种情况进行讨论,n为奇数时,开始扫描方向为左下角,n为偶数时,开始扫描方向为右上角


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值