「java螺旋矩阵」java螺旋矩阵从右上角开始

博主:adminadmin 2022-12-03 17:48:07 93

本篇文章给大家谈谈java螺旋矩阵,以及java螺旋矩阵从右上角开始对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

求用java语言输出n*m阶螺旋矩阵

height就相当于n

width就相当于m

public class SpiralMatrixMaker {

public static void main(String[] args) {

int max = 20;

int i = 0, j = 0;

int height = 3;

int width = 4;

int[][] a = new int[max][max];

int m = 1, s = 1;

int x1 = 0;

int y1 = 0;

int x2 = height;

int y2 = width;

while (true) {

if (s == 1) {

for (; j y2; j++) {

a[i][j] = m++;

}

j--;

i++;

y2--;

for (; i x2; i++) {

a[i][j] = m++;

}

i--;

j--;

x2--;

s = -1;

} else {

for (; j = y1; j--) {

a[i][j] = m++;

}

j++;

i--;

y1++;

for (; i = x1 + 1; i--) {

a[i][j] = m++;

}

i++;

j++;

x1++;

s = 1;

}

if (m height * width) {

break;

}

}

for (i = 0; i height; i++) {

for (j = 0; j width; j++) {

System.out.print(a[i][j]);

System.out.print("\t");

}

System.out.println();

}

}

}

java螺旋矩阵求助!

package cn.com.micc.javatwo; //根据实际情况修改

//蜗牛螺旋矩阵 请仔细研究矩阵阶数变化时数据的迁移规律

//上一阶矩阵会"整体"向右上或左下移动

public class AntiClockWiseArray {

public static int[][] getResult(int n) {

int[][] n1 = new int[1][1];

n1[0][0] = 1;

if (n == 1)

return n1;

int[][] result = new int[n][n];

int[][] temp = getResult(n - 1);

if (0 == (n - 1) % 2)

result = LeftDownMove(temp, n - 1); //n-1阶矩阵向左下移动

else

result = RightUpMove(temp, n - 1); //n-1阶矩阵向右上移动

return result;

}

public static int[][] LeftDownMove(int[][] in, int moment) {

int temp = moment * moment;

int nums = moment * 2 + 1;

int[][] out = new int[moment + 1][moment + 1];

//新矩阵补入上一阶矩阵的值

for (int i = 0; i moment; ++i)

for (int j = 0; j moment; ++j)

out[i + 1][j] = in[i][j];

//两个循环添加新矩阵新值

for (int k = 0; k moment + 1; ++k)

out[0][k] = temp + nums - k;

for (int l = 1; l moment + 1; ++l)

out[l][moment] = temp + nums - moment - l;

return out;

}

public static int[][] RightUpMove(int[][] in, int moment) {

int temp = moment * moment;

int nums = moment * 2 + 1;

int[][] out = new int[moment + 1][moment + 1];

//新矩阵补入上一阶矩阵的值

for (int i = 0; i moment; ++i)

for (int j = 0; j moment; ++j)

out[i][j + 1] = in[i][j];

//两个循环添加新矩阵新值

for (int k = 0; k moment + 1; ++k)

out[k][0] = temp + 1 + k;

for (int l = 1; l moment + 1; ++l)

out[moment][l] = temp + moment + 1 + l;

return out;

}

public static void printArray(int[][] temp, int n) {

//格式化打印矩阵

for(int i = 0; i n; ++i)

{

for(int j = 0; j n; ++j)

System.out.printf("%5d", temp[i][j]);

System.out.println();

}

}

public static void main(String[] args) {

printArray(getResult(6), 6); //输入阶数

}

}

output:

10阶

82 81 80 79 78 77 76 75 74 73

83 50 49 48 47 46 45 44 43 72

84 51 26 25 24 23 22 21 42 71

85 52 27 10 9 8 7 20 41 70

86 53 28 11 2 1 6 19 40 69

87 54 29 12 3 4 5 18 39 68

88 55 30 13 14 15 16 17 38 67

89 56 31 32 33 34 35 36 37 66

90 57 58 59 60 61 62 63 64 65

91 92 93 94 95 96 97 98 99100

如何用JAVA实现螺旋矩阵

import java.io.*;public class RingDemo {

public static void main(String[] args) {

String strIn = "";

System.out.print("请输入矩阵的行列数:");

InputStreamReader input = new InputStreamReader(System.in);

BufferedReader buff = new BufferedReader(input);

try {

strIn = buff.readLine();

} catch (IOException e) {

System.out.println(e.toString());

}

int int1 = Integer.parseInt(strIn);

int n = int1;

System.out.println("这是行列数为" + n + "的螺线型数组:");

int intA = 1; // 初始化

int[][] array = new int[n][n];

int intB;

if (n % 2 != 0) {

intB = n / 2 + 1; // 奇数时i循环次数

} else

intB = n / 2; // 偶数时i循环次数

for (int i = 0; i intB; i++) { // 从外到里循环

// 从左到右横的开始

for (int j = i; j n - i; j++) {

array[i][j] = intA;

intA++;

}

// 从上到下纵

for (int k = i + 1; k n - i; k++) {

array[k][n - i - 1] = intA;

intA++;

}

// 从右到左横

for (int l = n - i - 2; l = i; l--) {

array[n - i - 1][l] = intA;

intA++;

}

// 从下到上纵

for (int m = n - i - 2; m i; m--) {

array[m][i] = intA;

intA++;

}

}

// 输出数组

for (int i = 0; i n; i++) {

for (int j = 0; j n; j++) {

System.out.print(array[i][j] + " ");

}

System.out.println();

} }

}

怎么编写JAVA螺旋矩阵?

按照你的要求用Java编写的螺旋矩阵程序如下:

public class N {

public static void main(String[] args) {

final int N=4;

int a[][]=new int[N][N];

int num=1;

int i=0,j=0,m=0;

if(N%2==0)

m=N/2;

else

m=N/2+1;

for(i=0;i=m-1;i++){

for(j=i;j=N-i-1;j++){

a[i][j]=num;

num++;

}

for(j=i+1;j=N-i-1;j++) {

a[j][N-i-1]=num;

num++;

}

for(j=N-i-2;j=i;j--){

a[N-i-1][j]=num;

num++;

}

for(j=N-i-2;j=i+1;j--){

a[j][i]=num;

num++;

}

}

for(i=0;iN;i++){

for(j=0;jN;j++){

System.out.print(String.format("%3d",a[i][j]));

}

System.out.println();

}

}

}

运行结果:

1 2 3 4

12 13 14 5

11 16 15 6

10 9 8 7

急!求JAVA螺旋矩阵的N*N算法(必须能运行的!!)

呵呵,大一做过的

package bao;

public class Juzhen {

public static void main(String[] args){

int k=1,i=0,j=0,m=0;

System.out.println("输入矩阵大小:");

int n=SavitchIn.readLineInt();

int[][] A=new int[n][n];

for(m=0;mn;m++){

for(i=m,j=m;jn-m;j++){

A[i][j]=k;k++;

}j--;

for(i=i+1;in-m;i++){

A[i][j]=k;k++;

}i--;

for(j=j-1;j=m;j--){

A[i][j]=k;k++;

}j++;

for(i=i-1;im;i--){

A[i][j]=k;k++;

}i++;

}

for(int a=0;an;a++){

for(int b=0;bn;b++){

System.out.print(A[a][b]+"\t");

}

System.out.println();

}

}

}

其中SavitchIn为

public class SavitchIn

{

/***

*Reads a line of text and returns that line as a String value.

*The end of a line must be indicated either by a new-line

*character '\n' or by a carriage return '\r' followed by a

*new-line character '\n'. (Almost all systems do this

*automatically. So, you need not worry about this detail.)

*Neither the '\n', nor the '\r' if present, are part of the

*string returned. This will read the rest of a line if the

*line is already partially read.

****/

public static String readLine()

{

char nextChar;

String result = "";

boolean done = false;

while (!done)

{

nextChar = readChar();

if (nextChar == '\n')

done = true;

else if (nextChar == '\r')

{

//Do nothing.

//Next loop iteration will detect '\n'

}

else

result = result + nextChar;

}

return result;

}

public static int readLineInt()

{

String inputString = null;

int number = -9999;//To keep the compiler happy.

//Designed to look like a garbage value.

boolean done = false;

while (! done)

{

try

{

inputString = readLine();

inputString = inputString.trim();

number = Integer.parseInt(inputString);

done = true;

}

catch (NumberFormatException e)

{

System.out.println(

"Your input number is not correct.");

System.out.println("Your input number must be");

System.out.println("a whole number written as an");

System.out.println("ordinary numeral, such as 42");

System.out.println("Minus signs are OK,"

+ "but do not use a plus sign.");

System.out.println("Please, try again.");

System.out.println("Enter a whole number:");

}

}

return number;

}

}

关于java螺旋矩阵和java螺旋矩阵从右上角开始的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-03,除非注明,否则均为首码项目网原创文章,转载请注明出处。