一、蛇形矩阵的构建,并按行输出

例:

输入:n,

生成n*n的蛇形矩阵

1 2 3

8 9 4

7 6 5

输出:1 2 3 8 9 4 7 6 5

java编码

public static void main(String[] args) {
// TODO Auto-generated method stub Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n < 0)
return;
if(n == 1){
System.out.println(1);
return;
} int[][] m = new int[n][n];
//定义4个变量,分别记录当前行和列的上下限
int i1 = 0, j1 = 0, i2 = n - 1, j2 = n - 1;
int i = 0, j = 0;
int begin = 1;
//4个for循环,遍历蛇形矩阵
while(i1 <= i2 && j1 <= j2){ for(; j <= j2; ++j){//one row
m[i][j] = begin;
begin++;
}
j--;
i++;
i1++; for(; i <= i2; ++i){//one column
m[i][j] = begin;
begin++;
}
j2--;
j--;
i--; for(; j >= j1; --j){//one row
m[i][j] = begin;
begin++;
}
i2--;
j++;
i--; for(; i >= i1; --i){//one column
m[i][j] = begin;
begin++;
}
j1++;
j++;
i++;
} //按行输出 蛇形矩阵
for(i = 0; i < n; ++i)
for(j = 0; j < n; ++j)
System.out.println(m[i][j] + " ");
}

二、已知蛇形矩阵m,顺时针顺序输出

例:

矩阵m

1   2   3   4

12 13 14  5

11 16 15  6

10 9   8   7

输出:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

public static void main(String[] args) {
// TODO Auto-generated method stub int[][] m = {{1,2,3,4},{12,13,14,5},{11,16,15,6},{10,9,8,7}};
int n = m.length;
int c = m[0].length;
if(n == 1){
for(int a = 0; a < m[0].length; ++a)
System.out.print(m[0][a] + " ");
}
if(c == 1){
for(int a = 0; a < m.length; ++a)
System.out.print(m[a][0] + " ");
} //定义4个指针,记录行和列的上下限
int i1 = 0, j1 = 0, i2 = n - 1,j2 = c - 1;
int i = 0, j = 0;
while(i1 <= i2 && j1 <= j2){//one row
for(; j <= j2; ++j)
System.out.print(m[i][j] + " ");
i++;
j--;
i1++;
for(; i <= i2; ++i)//one column
System.out.print(m[i][j] + " ");
i--;
j--;
j2--;
for(; j >= j1; --j)//one row
System.out.print(m[i][j] + " ");
i--;
j++;
i2--;
for(; i >= i1; --i)//one column
System.out.print(m[i][j] + " ");
i++;
j++;
j1++;
} }

Java编码 蛇形矩阵的构建与遍历输出的更多相关文章

  1. 【JAVA-JDT-AST】Java抽象语法树的构建、遍历及转成dot格式(附Github源码)

    Background: 最近为了重现tree-based clone detection的论文:L. Jiang, G. Misherghi, Z. Su, and S. Glondu. Deckar ...

  2. Java使用foreach语句对数组成员遍历输出

    /** * 本程序使用foreach语句对数组成员进行遍历输出 * @author Lei * @version 2018-7-23 */ public class ForeachDemo { pub ...

  3. Java实现蛇形矩阵

    public class Solution { //下x++ 左y-- 上x-- 右y++ public void prints(int n) { int[][] mp = new int[n][n] ...

  4. java递归实现文件夹文件的遍历输出

    学习java后对一个面试小题(今年年初在团结湖面试的一个题目) 的习题的编写. ''给你一个文件,判断这个文件是否是目录,是目录则输入当前目录文件的个数和路径,''' /** * @author li ...

  5. Java类的调用(实现数组排序和遍历输出)

    两个类文件: Test1.java /** *同一个src下的两个类,主类在这里,调用另一个文件里的Public类 */ import java.lang.*; public class Test1 ...

  6. js实现蛇形矩阵

    参加腾讯前端实习生笔试,真的是被虐了千百遍,除了一条js程序题,其他半点前端都没有,都是考算法,计算机原理,数据结构.下面贴上腾讯笔试最后三大条中的一条,实现一个蛇形矩阵的输出.蛇形矩阵的什么样这里我 ...

  7. 【面试】输出"蛇形"矩阵

    一.题目描述 腾讯实习在线笔试的一道题目. 根据输入的数字(< 1000),输出这样的"蛇形"矩阵,如下.输入n,输出(n * n)阶矩阵,满足由外到内依次增大. 如: 输入 ...

  8. 面向 Java 开发人员的 Ajax: 构建动态的 Java 应用程序

    面向 Java 开发人员的 Ajax: 构建动态的 Java 应用程序 Ajax 为更好的 Web 应用程序铺平了道路 在 Web 应用程序开发中,页面重载循环是最大的一个使用障碍,对于 Java™ ...

  9. 【JAVA编码专题】总结

    第一部分:编码基础 为什么需要编码:用计算机看得懂的语言(二进制数)表示各种各样的字符. 一.基本概念 ASCII.Unicode.big5.GBK等为字符集,它们只定义了这个字符集内有哪些字符,以及 ...

随机推荐

  1. P5057 [CQOI2006]简单题(线段树)

    果然简单题,5分钟紫题++ 代码 #include <cstdio> #include <algorithm> #include <cstring> using n ...

  2. Unity3D学习笔记(二十九):AssetBundle

    AssetBundle 什么是AssetBundle? AssetBundle是把一些资源文件或场景文件,以某种方式保存在一个文件中.一个AssetBundle可以包含模型.材质.图片或场景等.但是A ...

  3. facebook api之Marketing API

    General information on the Marketing APIs, access, versioning and more. The main use cases for the M ...

  4. 操作 frames 框架下的 dom 内容。

    2016-12-30 框架名: var obj=$(window.frames["wangpan"].document).find("a[menu=download_on ...

  5. React内三种函数的写法

     以下提供三种React内函数的写法,都可以正常运行,有疑问可以留言 写法一:让函数内部的this指向这个类的实例,它是用bind实现的,bind的第一个参数表示context,就是this. //写 ...

  6. git pull 提示 There is no tracking information for the current branch

    在执行git pull的时候,提示当前branch没有跟踪信息: git pull There is no tracking information for the current branch. P ...

  7. string截取、替换、查找子串函数,find_first_of 用法

    1. 截取子串 s.substr(pos, n) 截取s中从pos开始(包括0)的n个字符的子串,并返回 s.substr(pos) 截取s中从从pos开始(包括0)到末尾的所有字符的子串,并返回 2 ...

  8. Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模

    E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. django 应用中获取访问者ip地址

    通常访问者的IP就在其中,所以我们可以用下列方法获取用户的真实IP: #X-Forwarded-For:简称XFF头,它代表客户端,也就是HTTP的请求端真实的IP,只有在通过了HTTP 代理或者负载 ...

  10. IIS附加进程调试