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 ...
随机推荐
- Selectize使用总结
一.简介 Selectize是一个可扩展的基于jQuery 的自定义下拉框的UI控件.它对展示标签.联系人列表.国家选择器等比较有用.它的大小在~ 7kb(gzip压缩)左右.提供一个可靠且体验良好的 ...
- JS中将一个值转换为字符串的3种方法
1.value.toString() 2."" + value 3.String(value) 第一种方法存在的问题是,它不能把null和undefined转换为字符串.还有第二种 ...
- 【HTTP协议】
一.简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议 ...
- QuickStart系列:docker部署之Elasticsearch
ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...
- [Spark内核] 第38课:BlockManager架构原理、运行流程图和源码解密
本课主题 BlockManager 运行實例 BlockManager 原理流程图 BlockManager 源码解析 引言 BlockManager 是管理整个Spark运行时的数据读写的,当然也包 ...
- Windows和Linux下换行的不同
因为测试IM会用到一些账号,于是写了一段代码从数据库里把需要的用户名.密码和手机号都一一取了出来,然后放到NotePad++中做进一步的处理. 取用户名.手机号和密码的代码如下: public cla ...
- MySQL5.7新特性:lossless replication 无损复制
MySQL的三种复制方式 asynchronous 异步复制 fully synchronous 全同步复制 Semisynchronous 半同步复制 asynchronous replicatio ...
- my discipline life
周一 ~ 周五 7.30 --- 8.00 起床,洗漱等 8.00 --- 8.30 默写8-10个英语单词 8.35 --- 9.10 吃早餐,骑自行车去公司 12.20 --- 1 ...
- flexbox 弹性盒子
flexbox 弹性盒子 1.基本知识 container(容器)属性 flex-direction: row | row-reverse | column | column-reverse 属性决定 ...
- kafka快速入门(官方文档)
第1步:下载代码 下载 1.0.0版本并解压缩. > tar -xzf kafka_2.11-1.0.0.tgz > cd kafka_2.11-1.0.0 第2步:启动服务器 Kafka ...