「java螺旋矩阵规律」螺旋矩阵原理
今天给各位分享java螺旋矩阵规律的知识,其中也会对螺旋矩阵原理进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java螺旋矩阵求助!
- 2、如何用JAVA实现螺旋矩阵
- 3、谁有螺旋矩阵的说明?
- 4、求编写JAVA螺旋矩阵
- 5、java二维数组3*3螺旋矩阵实例,请解释下循环的过程
- 6、急!求JAVA螺旋矩阵的N*N算法(必须能运行的!!)
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();
} }
}
谁有螺旋矩阵的说明?
关于螺旋矩阵的说法不一,这里指的是形如
21 22................
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
的矩阵。
问题有两个:
1. 编程实现输出这个矩阵
2. 设1点的坐标是(0,0),x方向向右为正,y方向向下为正.例如:7的坐标为(-1,-1) ,2的坐标为(0,1),3的坐标为(1,1).编程实现输入任意一点坐标(x,y),输出所对应的数字。
1. 第一个问题我是采用模拟进行构造的,可以看到从1开始的方向变化始终是 right-down-left-up,
所持续走的长度为1-1-2-2-3-3-...,发现了这个规律不难写出代码了!注意下面我把1的位置设置
在((n-1)/2, (n-1)/2)的位置。
void Simulate(int n)
{
int x, y;
x = y = (n - 1) / 2; //1的位置
data[x][y] = 1;
int len = 1;
int count = 0;
int num = 2;
DIRECTION dir = RIGHT;
while(num = n * n)
{
for(int i = 0; i len; i++)
{
switch(dir)
{
case LEFT:
--y; break;
case RIGHT:
++y; break;
case UP:
--x; break;
case DOWN:
++x; break;
default: break;
}
data[x][y] = num++;
}
count++;
if(count == 2)
{
count = 0;
len++;
}
dir = (DIRECTION)((dir + 1) % 4);
}
}
2. 第二个问题我也是先找出规律,然后进行模拟。
首先,不难看出n*n的螺旋矩阵的右下角的坐标一定是(m, m),这里m=n-1
通过观察,可以看出 n=1的时候,右下角(0,0)的值为1,当n=2的时候,右下角(1,1)的坐标值为(3,3),当n=3的时候,右下角(2,2)的坐标值为13.直觉告诉我,这个值是关于n的二次函数,设f(n) = a*n^2 + b*n + c
联立方程组,可以求得a,b,c。 最终算出来的f(n) = 4*n^2 - 2*n + 1
下面再根据(x,y)和右下角(n-1,n-1)之间的关系,计算出值即可。这里要注意当x的值与n-1相同时,应优先考虑y与-m是否有联系。这就要求在函数中要注意x,y的判断先后顺序了。
代码如下:
//以(1,1)所在位置作为原点,向右作为x正半轴,向下作为y正半轴
int GetValue(int x, int y)
{
int m = max(abs(x), abs(y));
int rightBottom = m * m * 4 - 2 * m + 1;
int value = 0;
if(x == -m)
{
value = rightBottom + 2 * m + m - y;
}
else if( y == m)
{
value = rightBottom + m - x;
}
else if(y == -m)
{
value = rightBottom + 4 * m + x + m;
}
else if( x == m )
{
value = rightBottom - (m - y);
}
return value;
}
求编写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二维数组3*3螺旋矩阵实例,请解释下循环的过程
public class T {
public static void main(String[] args) {
int i,j;
int[][] a = {{1,1,1},{2,2,2},{3,3,3}};
int[][] b = new int[3][3];
System.out.print("初始矩阵:\n");
for(i=0;i3;i++) {
for(j=0;j3;j++) {
System.out.print(a[i][j]+" ");
}
System.out.print("\n");
}
System.out.print("转置矩阵:\n");
for(i=0;i3;i++) {
for(j=0;j3;j++) {
b[i][j] = a[j][i];
System.out.print(b[i][j]+" ");
}
System.out.print("\n");
}
}
}
急!求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螺旋矩阵规律的信息别忘了在本站进行查找喔。