CCF系列之Z字形扫描(201412-2)
试题名称:Z字形扫描
时间限制: 2.0s
内存限制: 256.0MB

对于下面的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+1行每行包含n个正整数,由空格分隔,表示给定的矩阵。
1 5 3 9
3 7 5 6
9 4 6 4
7 3 1 3






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)的更多相关文章
- CCF CSP 201412-2 Z字形扫描
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201412-2 Z字形扫描 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫 ...
- [CCF] Z字形扫描
CCF Z字形扫描 感觉和LeetCode中的ZigZag还是有一些不一样的. 题目描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z ...
- CCF——Z字形扫描问题
试题编号: 201412-2 试题名称: Z字形扫描 时间限制: 2.0s 内存限制: 256.0MB 问题描述: 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag ...
- CCF真题之Z字形扫描
201412-2 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 ...
- CSP201412-2:Z字形扫描
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...
- Z字形扫描(201412-2)
问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...
- Z字形扫描矩阵
问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...
- 201412-2 Z字形扫描(c语言)
问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...
- CCF201412-2 Z字形扫描 java(100分)
试题编号: 201412-2 试题名称: Z字形扫描 时间限制: 2.0s 内存限制: 256.0MB 问题描述: 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag ...
随机推荐
- ubuntu16.04编译安装php7.2
1,下载解压 tar xf php-7.2.0.tar.gz cd php-7.2.0/ 2,安装必要的库 sudo apt-get install libxml2-devsudo apt-get i ...
- php 面向对象三大特点:封装、继承、多态
在讲解这三大特性前,我们先讲访问修饰符. php中有3中访问修饰符:public protected private: public:表示公有的:可在本类.子类.对象实例中访问. protected: ...
- File System 之本地文件系统
上一篇文章提到了,最近做一个基于 File System/IndexedDB的应用,上一篇是定额和使用的查询. 因为LocalFileSystem只有chrome支持,有点尴尬,如果按需加载又何来尴尬 ...
- 云计算之路-阿里云上: RDS实例CPU跑满引发的故障
今天上午 10: 40 左右,我们所使用的阿里云 RDS 实例的 CPU 突然飙高到近 100% ,造成大量数据库查询操作缓慢.超时,在这个恶劣条件下大量 memcached 缓存无法建立,这样的雪上 ...
- NanUI文档 - 如何实现C#与Javascript的相互通信
NanUI文档目录 NanUI简介 开始使用NanUI 打包并使用内嵌式的HTML/CSS/JS资源 使用网页来设计整个窗口 如何实现C#与Javascript的相互通信 如何处理NanUI中的下载过 ...
- 【数论】洛谷P1414又是毕业季II
题目背景 "叮铃铃铃",随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘逝去的歌.1000多个日夜的欢笑和泪水,全凝聚在毕业 ...
- 机器学习系统设计(Building Machine Learning Systems with Python)- Willi Richert Luis Pedro Coelho
机器学习系统设计(Building Machine Learning Systems with Python)- Willi Richert Luis Pedro Coelho 总述 本书是 2014 ...
- 深入理解JVM(五)——垃圾回收器
轻松学习JVM(五)——垃圾回收器 上一篇我们介绍了常见的垃圾回收算法,不同的算法各有各的优缺点,在JVM中并不是单纯的使用某一种算法进行垃圾回收,而是将不同的垃圾回收算法包装在不同的垃圾回收器当中, ...
- PostgreSQL查询优化之子查询优化
子查询优化 上拉子连接 上拉子连接主要是把ANY和EXIST子句转换为半连接 void pull_up_sublinks(PlannerInfo *root) { Node *jtnode; //子连 ...
- webpack自定义打包命令
更快捷的执行打包任务 1.执行类似于node_modules/.bin/webpack这样的命令其实是比较烦人且容易出错的,不过值得庆幸的是npm可以引导任务执行, 对其进行配置后可以使用简单的npm ...