Topic: 今天看见一道不错的题目.......................

  Print this page

1.今天看见一道不错的题目....................... Copy to clipboard
Posted by: aiff
Posted on: 2004-09-30 21:46

1 3 6 10
2 5 9 13
4 8 12 15
7 11 14 16 这个程序用JAVA怎么写啊 呵呵

2.Re:今天看见一道不错的题目....................... [Re: aiff] Copy to clipboard
Posted by: why
Posted on: 2004-09-30 23:24

{ 请尽量用准确的文字描述作为标题 }


What are you talking about?QuestionQuestionQuestion

3.Re:今天看见一道不错的题目....................... [Re: aiff] Copy to clipboard
Posted by: justin_here
Posted on: 2004-09-30 23:39

左下到右上依序排列?

4.Re:今天看见一道不错的题目....................... [Re: aiff] Copy to clipboard
Posted by: aiff
Posted on: 2004-10-01 00:31

结果输出为
1 3 6 10
2 5 9 13
4 8 12 15
7 11 14 16
的程序 怎么写?有什么新方法

5.Re:今天看见一道不错的题目....................... [Re: aiff] Copy to clipboard
Posted by: sankxuan
Posted on: 2004-10-01 14:51

public class IntQueue {
public static void main(String[] args) {
int xIncrease = 1;
int yIncrease = 1;
for (int i = 0; i < 4; i++) {
xIncrease = yIncrease + i;
for (int j = 0; j < 4; j++) {
xIncrease += j;
int printNum = j + xIncrease;
System.out.print(printNum + " ");
}
yIncrease ++;
System.out.print("\n");
}
}
}

6.Re:今天看见一道不错的题目....................... [Re: aiff] Copy to clipboard
Posted by: aiff
Posted on: 2004-10-01 16:13

楼上的 你的code不对啊~~你检查检查

7.Re:今天看见一道不错的题目....................... [Re: aiff] Copy to clipboard
Posted by: sankxuan
Posted on: 2004-10-01 20:52

对不起,看错题目意思了,改了改

/**
*
*
* @author sank
*/
public class IntQueue {
static int[][] matrix;

public int[][] getMatrix(int size) {
matrix = new int[size][size];
int row = 0;
Position point = new Position(0, 0);
for (int i = 1; i < size * size + 1; i++) {
matrix[point.y][point.x] = i;
point = findNextPostion(point,size);
}
return matrix;
}

Position findNextPostion(Position p,int size) {
Position rtl = new Position(p.x,p.y);
if (rtl.y != 0 && rtl.x != size - 1) {
rtl.y = rtl.y -1;
rtl.x = rtl.x +1;
} else {
int backSize = rtl.x;
for (int i = 0; i < backSize; i++) {
rtl.y ++;
rtl.x --;
if (rtl.y == size - 1 || rtl.x == 0) {
break;
}
}
if (rtl.y == size - 1) {
rtl.x ++;
}
if (rtl.x == 0 && rtl.y != size - 1) {
rtl.y ++;
}
}
return rtl;
}

public static void main(String[] args) {
int size = 4;
IntQueue intQueue = new IntQueue();
matrix = intQueue.getMatrix(size);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.print("\n");
}
}
}

class Position {
public int x;
public int y;
Position() {
this(0,0);
}
Position(int x, int y) {
this.x = x;
this.y = y;
}
}

8.Re:今天看见一道不错的题目....................... [Re: aiff] Copy to clipboard
Posted by: dorrenchen
Posted on: 2004-10-02 00:10


/* this class print a square matrix diagonally, like this
* 1 3 6 10
* 2 5 9 13
* 4 8 12 15
* 7 11 14 16
*/
public class Diagonal {
  public static void main(String[] args) {
    int size = 4; // size can be from 1 - infinity
    int m[][] = new int[size][size];
    
    int last = 0;
    for(int i=0; i<2*size-1; i++){
      int max = i < size ? i+1 : size*2-1 - i; // element count in this slice
      int r = i < size ? i : size-1; // row
      int c = i - r; // column
      for(int j=0; j<max; j++){
        //System.out.println(r+","+c+":"+last + " i=" +i+", j="+j+", m="+ max);
        m[r--][c++]= ++last;
      }
    }
    
    for(int i=0; i<size; i++){
      for(int j=0; j<size; j++)
        System.out.print(m[i][j] + " ");
      System.out.println();
    }
  }
}


9.Re:今天看见一道不错的题目....................... [Re: aiff] Copy to clipboard
Posted by: kulatasana
Posted on: 2004-10-20 22:18

int size = 4;
int num[size][size];
int n = 1;
for(int i = 0;i < size;i++)
for(int j = 0;j < i+1;j++)
for(int k = 0;k < =j;k++){
num[k][j-k] = n;
num[size - k-1][size-j+k-1] = size*size-n;
n++;
}

应该没错,具体没调试,有错别怪我哦~~~
不过就算法应该没问题,循环次数也尽量做到最少了(1+size)*size/2次.

10.Re:今天看见一道不错的题目....................... [Re: aiff] Copy to clipboard
Posted by: kulatasana
Posted on: 2004-10-20 22:51

上面的代码似乎有点问题,,不小心多+了个循环,我的错,改过!!
int size = 4;
int num[size][size];
int n = 1;
for(int i = 0;i < size;i++)
for(int k = 0;k < i+1;k++){
num[k][i-k] = n;
num[size-k-1][size-i+k-1] = size*size+1-n;
n++;
}

不知道对不Smile


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923