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 ...
随机推荐
- Android真机安装sqlite3的方法
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- CSS3的动画属性
transition.animation和transform是CSS3中三个制作动画的重要属性,本篇文章主要对其进行学习了解. 一.transition transition允许css的属性值在一定的 ...
- golang 用tar打包文件或文件夹
打包文件用到了tar包,其中tar包的用法可以参考API golang提供了个函数用来遍历文件夹 filepath.Walk 函数具体描述如下: func Walk(root string, walk ...
- TurnipBit口袋编程计算机:和孩子一起DIY许愿的流星
听说对着流星许愿,许的愿望都会实现,虽然不知道这个说法是不是真的,但是流星还是很好看的,为了能一直看到流星,今天就自己做一个流星保存下来,想什么时候看,就什么时候看. 首先需要想象一下流星是什么样子的 ...
- 腾讯云负载均衡CLB的那些“独门利器”
欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者:李想 腾讯人做产品一直是很贴近用户的需求的,腾讯云也不例外.负载均衡器作为公有云上的最基础的网络服务,几乎每家云厂商都会提供,虽然负载均衡 ...
- 简单认识python cmd模块
0X00 前言 在早前用别人的工具时,发现有些大佬会用到交互式shell,那时候就挺好奇的,但是一直都没有看一下怎么做到的. 今天在翻p牛的博客的时候,看到他早之前写的一个工具就有用到交互式shell ...
- android开发遇到的问题
1.虚拟机运行出下面的错Failed to allocate memory: 8 Failed to allocate memory: 8This application has requested ...
- 数据结构-堆 C与C++的实现
堆,是一种完全二叉树.而且在这颗树中,父节点必然大于(对于小顶堆为小于)子节点. 关于树的概念不了解可以看这里:http://www.cnblogs.com/HongYi-Liang/p/723144 ...
- 关于js中的json对象,json串,数组之间相互转换
将json对象转换成string var loginUser = {username: username, password: password}//方式一 localStorage.setItem( ...
- 【Docker】安装Docker及基本使用
该文以CentOS系统为例,介绍Docker安装及基本使用.为了简化安装流程,Docker 官方提供了一套安装脚本,CentOS 系统上可以使用这套脚本安装: curl -sSL https://ge ...