试题编号: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. Android真机安装sqlite3的方法

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  2. CSS3的动画属性

    transition.animation和transform是CSS3中三个制作动画的重要属性,本篇文章主要对其进行学习了解. 一.transition transition允许css的属性值在一定的 ...

  3. golang 用tar打包文件或文件夹

    打包文件用到了tar包,其中tar包的用法可以参考API golang提供了个函数用来遍历文件夹 filepath.Walk 函数具体描述如下: func Walk(root string, walk ...

  4. TurnipBit口袋编程计算机:和孩子一起DIY许愿的流星

    听说对着流星许愿,许的愿望都会实现,虽然不知道这个说法是不是真的,但是流星还是很好看的,为了能一直看到流星,今天就自己做一个流星保存下来,想什么时候看,就什么时候看. 首先需要想象一下流星是什么样子的 ...

  5. 腾讯云负载均衡CLB的那些“独门利器”

    欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者:李想 腾讯人做产品一直是很贴近用户的需求的,腾讯云也不例外.负载均衡器作为公有云上的最基础的网络服务,几乎每家云厂商都会提供,虽然负载均衡 ...

  6. 简单认识python cmd模块

    0X00 前言 在早前用别人的工具时,发现有些大佬会用到交互式shell,那时候就挺好奇的,但是一直都没有看一下怎么做到的. 今天在翻p牛的博客的时候,看到他早之前写的一个工具就有用到交互式shell ...

  7. android开发遇到的问题

    1.虚拟机运行出下面的错Failed to allocate memory: 8 Failed to allocate memory: 8This application has requested ...

  8. 数据结构-堆 C与C++的实现

    堆,是一种完全二叉树.而且在这颗树中,父节点必然大于(对于小顶堆为小于)子节点. 关于树的概念不了解可以看这里:http://www.cnblogs.com/HongYi-Liang/p/723144 ...

  9. 关于js中的json对象,json串,数组之间相互转换

    将json对象转换成string var loginUser = {username: username, password: password}//方式一 localStorage.setItem( ...

  10. 【Docker】安装Docker及基本使用

    该文以CentOS系统为例,介绍Docker安装及基本使用.为了简化安装流程,Docker 官方提供了一套安装脚本,CentOS 系统上可以使用这套脚本安装: curl -sSL https://ge ...