试题编号:201412-2
试题名称:Z字形扫描
时间限制: 2.0s
内存限制: 256.0MB
问题描述
  在图像编码的算法中,需要将一个给定的方形矩阵进行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的正整数。
 
解题思路:
 
 
 
 
 
 
 
实现代码自己写的(java):
  

 import java.util.Scanner;

 public class Main {
public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); sc.nextLine(); int num = n + 1; int[][] matrix=new int[num][num]; for (int i = 1; i < num; i++) { for (int j = 1; j < num; j++) { matrix[i][j]=sc.nextInt();
}
sc.nextLine();
}
printIt(matrix);
} public static void printIt(int[][]arr){ int n = arr.length; n--; for(int i = 1; i<=n; i++){ if(i%2 == 1){ int k = 1; for(int j = i; j>0;j--){ System.out.print(arr[j][k++]+" ");
} }else{ int k = i; for(int j = 1; j<=i;j++){ System.out.print(arr[j][k--]+" ");
}
}
} for(int i = n-1; i>0; i--){ if(i%2 == 0){ int k = n; for(int j = n-i+1; j<=n;j++){ System.out.print(arr[j][k--]+" ");
} }else{ int k = n-i+1; for(int j = n; j>=n-i+1;j--){ System.out.print(arr[j][k++]+" ");
}
}
}
} }

运行结果:

  

实现代码2 别人写的(java):

  

 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系列之Z字形扫描(201412-2)的更多相关文章

  1. CCF CSP 201412-2 Z字形扫描

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201412-2 Z字形扫描 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫 ...

  2. [CCF] Z字形扫描

    CCF Z字形扫描 感觉和LeetCode中的ZigZag还是有一些不一样的. 题目描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z ...

  3. CCF——Z字形扫描问题

    试题编号: 201412-2 试题名称: Z字形扫描 时间限制: 2.0s 内存限制: 256.0MB 问题描述: 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag ...

  4. CCF真题之Z字形扫描

    201412-2 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 ...

  5. CSP201412-2:Z字形扫描

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  6. Z字形扫描(201412-2)

    问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...

  7. Z字形扫描矩阵

    问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...

  8. 201412-2 Z字形扫描(c语言)

    问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...

  9. CCF201412-2 Z字形扫描 java(100分)

    试题编号: 201412-2 试题名称: Z字形扫描 时间限制: 2.0s 内存限制: 256.0MB 问题描述: 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag ...

随机推荐

  1. 使用git工具将项目上传到github

    注册github账号 https://github.com/ 安装git工具: https://git-for-windows.github.io/ 上面的准备工作完成后,现在开始操作. 一.进入gi ...

  2. checkbox对齐-复选框图标

    checkbox对齐-复选框图标 一般开发过程中,我们直接使用<input type="checkbox"/>这样出现的复选框,设计师一般都说不好看 而让我们按照设计稿 ...

  3. 见招拆招-PostgreSQL中文全文索引效率优化

    * { color: #3e3e3e } body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans ...

  4. 【转】彻底理解js中this的指向,不必硬背。

    首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...

  5. [代码审计]青云客Cms前台有条件注入至getshell,后台xss至getshell、至弹你一脸计算器

    之前写了一篇关于青云客cms的文章,发在了t00ls,就不copy过来了. 给个链接,好记录一下. https://www.t00ls.net/thread-43093-1-1.html

  6. delphi各种错

    1. 保存文件form_spml时出上面的错,点yes后还是会出错. 解决:有时间要关闭delphi2006软件才会跳出“remove/redirect the links to another mo ...

  7. asp.net mvc 5 蛋疼的问题

    看图,debugger显示匹配路径没有错.    html开源码看 显示没有实现IController. 然而我的确实现了.  关闭vs 重启

  8. windows 驱动开发入门——驱动中的数据结构

    最近在学习驱动编程方面的内容,在这将自己的一些心得分享出来,供大家参考,与大家共同进步,本人学习驱动主要是通过两本书--<独钓寒江 windows安全编程> 和 <windows驱动 ...

  9. CSS(二) 颜色 盒模型 文字相关 border

    一.颜色 rgb(r,g,b)  rgb取值 0-255   分别是 色光三元色  red green blue rgba(r,g,b,a) rgb同上  a 是 alpha  代表透明度 colot ...

  10. 如何获取离线安装Chrome扩展程序的包

    最近工作环境限制,有些机器文件只能拿进去,不能拿出来.网络也是内部网络,没法下载东西.工作中常用的一些Chrome扩展应用也没法使用,对于我这类工具爱好者,打击够大.后来想想,既然扩展应用能开发打包上 ...