package topcoder;
/*
* 下面是一个5*5阶的螺旋方阵.试编程打印出此形式的n*n(n<10)阶的方阵(顺时针方向旋进).
* 1 2 3 4 5
* 16 17 18 19 6
* 15 24 25 20 7
* 14 23 22 21 8
* 13 12 11 10 9
*/
public class screwSquare
{
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("usage: \"java screwSquare integer.\"");
System.exit(0);
}
int i = Integer.parseInt(args[0]);
int[][] matrix = new int[i][i];
int max = i * i;
int row = 0, col = 0;
int direction = 0;
for (int j = 1; j <= max; j++) {
matrix[row][col] = j;
switch (direction) {
case 0:
if (col + 1 >= i || matrix[row][col + 1] > 0) {
direction += 1;
direction %= 4;
row += 1;
} else {
col = col + 1;
}
break;
case 1:
if (row + 1 >= i || matrix[row + 1][col] > 0) {
direction += 1;
direction %= 4;
col -= 1;
} else {
row = row + 1;
}
break;
case 2:
if (col - 1 < 0 || matrix[row][col - 1] > 0) {
direction += 1;
direction %= 4;
row = row - 1;
} else {
col = col - 1;
}
break;
case 3:
if (row - 1 < 0 || matrix[row - 1][col] > 0) {
direction += 1;
direction %= 4;
col += 1;
} else {
row = row - 1;
}
break;
default:
System.out.println("ERROR");
System.exit(0);
}
}
for (int j = 0; j < i; j++) {
for (int k = 0; k < i; k++) {
if (matrix[j][k] < 10)
System.out.print(" " + matrix[j][k]);
else
System.out.print(" " + matrix[j][k]);
}
System.out.println("");
}
}
}这是本人在网上找到的一个打印螺旋方阵的程序.有一个地方看不太懂.如switch-case语句中第一个判断方向的语句if (col + 1 >= i || matrix[row][col + 1] > 0) {
direction += 1;
direction %= 4;
row += 1;
} else {
col = col + 1;
}
break; 我知道if后边括号里的表达式是判断方向的,但我只能看懂col+1>=i后边一名 matrix[row][col + 1] > 0是何用途就看不懂了。郁闷ING请教高人
解决方案 »
- java编写服务器传送图片问题
- 两个大数相乘,帮忙修改下代码,急~ 在线等~
- 将"a.b.c.d"中的点符号转换成反斜杠
- 一个try catch的catch块问题
- 如何设置当前目录,我现在只能用绝对路径InputStream is = new FileInputStream(PROP_FILE_NAME);
- 急!~一个数据结构方面的问题,高手请进
- 我想用java访问sql server2000,怎么不行啊?请赐教。
- 如何通过applet的权限封锁,实现读取客户硬件断口?
- jbuilder访问sqlserver的简单问题,高分了!!!!!!!
- JAVA新手求解
- 华为技术面试压轴题,请说明java的类加载机制!
- 超乎想象的面试题,厉害,厉害。!!!
import java.text.DecimalFormat;
public class HelixMatrix {
private int[][] array;
private int m,n;
public HelixMatrix(int m, int n) {
this.m = m;
this.n = n;
array = new int[m][];
for(int i = 0; i < m; i++)
array[i] = new int[n];
} private void fill() {
int count = m * n;
int direct = 0;
int round = 1;
for(int index = 1, x = 0, y = 0; index <= count; index++) {
array[x][y] = index;
switch(direct) {
case 0: //向右
if(y < n - round)
y++;
else {
direct = 1;
x++;
}
break;
case 1:
if(x < m - round)
x++;
else {
direct = 2;
y--;
}
break;
case 2:
if(y >= round)
y--;
else {
direct = 3;
x--;
}
break;
case 3:
if(x > round)
x--;
else {
direct = 0;
round++;
y++;
}
break;
}
}
} private void printMatrix() {
DecimalFormat df = new DecimalFormat(" 000");
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++)
System.out.print(df.format(array[i][j]));
System.out.println();
}
}
public static void main(String[] args) {
HelixMatrix matrix = new HelixMatrix(8,6);
matrix.fill();
matrix.printMatrix();
}
}
public class LuoXuan{
class zuoBiao{
private int x;
private int y;
private zuoBiao next;
public zuoBiao(int m,int n){
this(m,n,null);
}
public zuoBiao(int m,int n,zuoBiao s){
x=m;
y=n;
next=s;
}
}
private zuoBiao head;
public LuoXuan(){
head=new zuoBiao(0,0);
}
public int OneCircle(int n){
zuoBiao temp=head;
int i=0,=n;
for(;n>=1;n=n-2,i=i+1){
temp.next=new zuoBiao(i,i);
temp=temp.next;
while(temp.y<n+i-1){
temp.next=new zuoBiao(temp.x,temp.y+1);
temp=temp.next;
}
while(temp.x<n+i-1){
temp.next=new zuoBiao(temp.x+1,temp.y);
temp=temp.next;
}
while(temp.y>i){
temp.next=new zuoBiao(temp.x,temp.y-1);
temp=temp.next;
}
while(temp.x>i+1){
temp.next=new zuoBiao(temp.x-1,temp.y);
temp=temp.next;
}
}
return ;
}
public void Print(int n){
int[][] Diagram=new int[n][n];
zuoBiao temp=head.next;
int i=1;
while(temp!=null){
int x=temp.x;
int y=temp.y;
Diagram[x][y]=i;
i++;
temp=temp.next;
}
for(int k=0;k<Diagram.length;k++){
for(int j=0;j<Diagram[k].length;j++)
System.out.print(Diagram[k][j]+"\t");
System.out.println();
}
}
public static void main(String[] args){
LuoXuan luoXuan1=new LuoXuan();
luoXuan1.Print(luoXuan1.OneCircle(6));
}
}
class zuoBiao{
private int x;
private int y;
private zuoBiao next;
public zuoBiao(int m,int n){
this(m,n,null);
}
public zuoBiao(int m,int n,zuoBiao s){
x=m;
y=n;
next=s;
}
}
private zuoBiao head;
public LuoXuan(){
head=new zuoBiao(0,0);
}
public int OneCircle(int n){
zuoBiao temp=head;
int i=0,=n;
for(;n>=1;n=n-2,i=i+1){
temp.next=new zuoBiao(i,i);
temp=temp.next;
while(temp.y<n+i-1){
temp.next=new zuoBiao(temp.x,temp.y+1);
temp=temp.next;
}
while(temp.x<n+i-1){
temp.next=new zuoBiao(temp.x+1,temp.y);
temp=temp.next;
}
while(temp.y>i){
temp.next=new zuoBiao(temp.x,temp.y-1);
temp=temp.next;
}
while(temp.x>i+1){
temp.next=new zuoBiao(temp.x-1,temp.y);
temp=temp.next;
}
}
return ;
}
public void Print(int n){
int[][] Diagram=new int[n][n];
zuoBiao temp=head.next;
int i=1;
while(temp!=null){
int x=temp.x;
int y=temp.y;
Diagram[x][y]=i;
i++;
temp=temp.next;
}
for(int k=0;k<Diagram.length;k++){
for(int j=0;j<Diagram[k].length;j++)
System.out.print(Diagram[k][j]+"\t");
System.out.println();
}
}
public static void main(String[] args){
LuoXuan luoXuan1=new LuoXuan();
luoXuan1.Print(luoXuan1.OneCircle(6));
}
}
轮回矩阵
阿长最近迷上了一种矩阵,他认为通过分析这种图形可以参悟人的生死轮回(狂汗)。
这个图形由1到n*n这些数字组成。n表示一个人的年龄。比如,当一个人的年龄为4的时候,
那么对于他的轮回矩阵就是如下的一个图形:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
从左上角的1开始,以顺时针的方向进行旋涡式的伸展。这样的一个图形我们称它为
4岁的轮回矩阵。为了更好的研究这些矩阵,阿长不得不再次求助于你,希望你能编
写一个程序,当我们输入一个人的年龄的时候,你的程序能生成一个对于该年龄轮回矩阵。输入:
一个数字n,表示年龄。对于30%的数据,n<=50,对于100%的数据,n<=500(汗,谁活了
这么久。)。输出:
轮回矩阵的结构图。每行的数字之间用一个空格分开,注意每行最后一个数字后面不要留有空格。
无须考虑数字的对齐问题。样例输入1:
4样例输出1:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7 样例输入2:
10 样例输出2:1 2 3 4 5 6 7 8 9 10
36 37 38 39 40 41 42 43 44 11
35 64 65 66 67 68 69 70 45 12
34 63 84 85 86 87 88 71 46 13
33 62 83 96 97 98 89 72 47 14
32 61 82 95 100 99 90 73 48 15
31 60 81 94 93 92 91 74 49 16
30 59 80 79 78 77 76 75 50 17
29 58 57 56 55 54 53 52 51 18
28 27 26 25 24 23 22 21 20 19【华中科技大学计算机学院第三届程序设计大赛试题】
*/import java.util.*;
import java.io.*;
public class arrayTest
{
public static void main(String[] arg) throws NumberFormatException, IOException
{
System.out.println("请输入N:");
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int x = Integer.parseInt(read.readLine());
int count = (x / 2) + 1;
int k = x;
int[][] a = new int[k][k];
for( int i = 1; i <= count; i++ )
{
for( int n = i - 1; n <= k - i; n++ )
{
if( i == 1 && n == 0 )
a[i-1][n] = 1;
else
a[i-1][n] = a[i-1][n-1] + 1;
}
for( int n = i; n <= k - i; n++ )
{
a[n][k-i] = a[n-1][k-i] + 1;
}
for( int n = k - i - 1; n >= i - 1; n-- )
{
a[k-i][n] = a[k-i][n+1] + 1;
}
for( int n = k - i - 1; n >= i; n-- )
{
a[n][i-1] = a[n+1][i-1] + 1;
}
}
for( int i = 0; i < k; i++ )
{
for( int j = 0; j < k; j++ )
{
if( a[i][j] <= 9 )
{
System.out.print(" "+a[i][j]);
}
else
System.out.print(" "+a[i][j]);
}
System.out.println();
}
}
}
拜托我是菜鸟.不要说太高深的东西了...
它的判断语句就是判断在这个方向上是不是已经走到头了,是不是要换方向了。
开始的手后,5*5的矩阵里都是0,一个个赋值。开始都是用col+1>=i这些判断就可以,但是到了下面这个情况
1 2 3 4 5
0 0 0 0 6
15 0 0 0 7
14 0 0 0 8
13 12 11 10 9
填上16以后,已经要转方向了,但并没有到头。也就是col-1<0不成立。
此时就是用matrix[row][col+1]> 0来判断。前面的算不算已经被赋值过了。
这样就可以把方向转掉了。
int w = 5;
int h = 5;
int[][] array = new int[w][h];
fill(array, 0, 0, w-1, h-1, 1);
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
System.out.printf("%2d ", array[x][y]);
}
System.out.println();
}
} public static void fill(int[][] array, int left, int top, int right, int bottom, int start) {
if (left > right || top > bottom) return; for (int x=left; x<=right; x++)
array[x][top] = start++; for (int y=top+1; y<=bottom; y++)
array[right][y] = start++; for (int x=right-1; x>=left; x--)
array[x][bottom] = start++; for (int y=bottom-1; y>=top+1; y--)
array[left][y] = start++; fill(array, left+1, top+1, right-1, bottom-1, start);
}
}
public class ScrewSquare { public static void main(String[] args) {
ScrewSquare o = new ScrewSquare();
o.fill();
o.print();
} int w, h;
int[][] array;
int left, top, right, bottom;
int start; public ScrewSquare() {
w = 5;
h = 5;
array = new int[w][h];
left = 0;
top = 0;
right = w - 1;
bottom = h - 1;
start = 1;
} public void fill() {
while (left <= right && top <= bottom) {
for (int x=left; x<=right; x++)
array[x][top] = start++; for (int y=top+1; y<=bottom; y++)
array[right][y] = start++; for (int x=right-1; x>=left; x--)
array[x][bottom] = start++; for (int y=bottom-1; y>=top+1; y--)
array[left][y] = start++; left ++; top ++; right --; bottom --;
}
} public void print() {
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
System.out.printf("%2d ", array[x][y]);
}
System.out.println();
}
}
}
用C编的,主要思想是
将a[i][j]用 i,j,n的函数表示
给定 i,j,n想办法用数学的方法算出该位上的数字#include<stdio.h>
#include<math.h>
#include<stdlib.h>int getElement(int i,int j,int n){
int mid = n/2;
int k = min( min(i,j) ,n -1 - max(i,j));
int sum = 4 * ( k * (n + 1) - k * (k+1));
if( j >= i) sum += (i+j) - 2*k + 1;
else sum += ( n - 2*k - 1) * 4 - (i+j) + 2*k + 1;
return sum;
}int main(){
int n,i,j; printf(" n = ");
scanf("%d",&n); for(i = 0 ; i < n;i++){
for(j = 0 ; j < n;j++)
printf("%5d ",getElement(i,j,n));
printf("\n");
}
return 0;
}
{
int n = 0;
public ScrewSquare(int n){
this.n = n;
}
private int getElement(int i,int j){
int mid = n/2;
int k = Math.min( Math.min(i,j) ,n -1 - Math.max(i,j));
int sum = 4 * ( k * (n + 1) - k * (k+1));
if( j >= i) sum += (i+j) - 2*k + 1;
else sum += ( n - 2*k - 1) * 4 - (i+j) + 2*k + 1;
return sum;
}
public void print(){
for(int i = 0 ; i < n;i++){
for(int j = 0; j < n;j++)
System.out.print(" " + getElement(i,j));
System.out.println();
}
}
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("usage: \"java screwSquare integer.\"");
System.exit(0);
}
int n = Integer.parseInt(args[0]);
new ScrewSquare(n).print();
}
}
int n=10;
int[][] arr = new int[n][n];
int[][] add = {{0,1}, {1,0}, {0,-1}, {-1,0}};
int addId = 0;
int nextRow = 0;
int nextCol = 0;
for(int i=1; i<=n*n; i++){
arr[nextRow][nextCol] = i;
nextRow = row+add[addId][0];
nextCol = col+add[addId][1];
if(nextRow < 0 || nextRow >= n || nextCol < 0 || nextCol >= n || arr[nextRow][nextCol] != 0){
addId = (addId + 1)%4;
nextRow = row+add[addId][0];
nextCol = col+add[addId][1];
}
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
System.out.println(arr[i][j] + " ");
}
System.out.println();
}
public static void main(String args[])
throws java.io.IOException{
int n;//矩阵维数
n=System.in.read()-'0';
int x,y,i=1;
int jz[][]=new int [n][n];
int dir =0;
//初始化
for(x=0;x<n;x++)//行
for(y=0;y<n;y++)
{
jz[x][y]=0; //i
//i++;
}
x=0;y=0;dir=0;
for(i=0;i<n*n;i++)
{
if(jz[x][y]!=0) //已填充数字
{
switch(dir)
{
case 0:
y--;
x++;
dir=1;
break;
case 1:
x--;
y--;
dir=2;
break;
case 2:
y++;
x--;
dir=3;
break;
case 3:
x++;
y++;
dir=0;
break;
}
}
if(jz[x][y]==0) //未填数字,填入
{
jz[x][y]=i+1;
switch(dir)
{
case 0:
y++;
if(y==n)
{
y--;x++;dir=1;
}
break;
case 1:
x++;
if(x==n)
{
x--;y--;dir=2;
}
break;
case 2:
if(y==0)
{
x--;dir=3;
}
else
y--;
break;
case 3:
if(x==0)
{
y++;dir=0;
}
else
x--;
break;
}
}
}
DecimalFormat df = new DecimalFormat(" 00"); //pri
for (x=0;x<n ;x++ )
{
for (y=0;y<n ;y++ ) {
System.out.print(df.format(jz[x][y]) + " ");
}
System.out.print("\n");
}
}
}
package assign4;import java.util.Scanner;public class luoxuan { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int [][]a;
a=new int[10][10];
int i,j,k,n;
System.out.println("Please input n(0<n<10):");
System.out.println("please one number:");
Scanner inputscan2 = new Scanner((System.in));
n = inputscan2.nextInt();
k=1;
for(i=0;i<n/2;i++)
{
for(j=i;j<n-i-1;j++) //1 1 1 1 2
a[i][j]=k++; //4 5 5 6 2
for(j=i;j<n-i-1;j++) //4 8 6 2
a[j][n-i-1]=k++; //4 8 7 7 2
for(j=n-i-1;j>i;j--) //4 3 3 3 3
a[n-i-1][j]=k++;
for(j=n-i-1;j>i;j--)
a[j][i]=k++;
}
if(n%2==1)
a[n/2][n/2]=k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
System.out.print(" "+a[i][j]);
System.out.println("\n");
}
}
}