都说csdn高手多,特来此寻求一算法的实现

Wuaner 2009-08-11 01:08:20
有个有maxRow行,maxColumn列的方阵,总共maxRow*maxColumn个元素,元素值从小到大为0,1,2,3,...maxRow*maxColumn-1。


算法目的:实现元素值的这样一种排序:

情况一:maxColumn为奇数(设maxRow=4,maxColumn=5):

18 16 15 17 19
13 11 10 12 14
8 6 5 7 9
3 1 0 2 4

即最终排序为:18 16 15 17 19 ... 3 1 0 2 4。

情况二:maxColumn为偶数(设maxRow=4,maxColumn=4):

14 12 13 15
10 8 9 11
6 4 5 7
2 0 1 3
即最终排序为:14 12 13 15 ...2 0 1 3。


谢谢大家的支持!!!!

...全文
355 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wuaner 2009-08-11
  • 打赏
  • 举报
回复
分不多,大家回答问题的积极使我不知道该怎么个分配这些分了。。。

大虫兄的热情是大家有目共睹的,我最终采用的就是大虫兄的办法;此外还有两位为我这个问题写了code做回复的,也一并散分感谢!
blliy117 2009-08-11
  • 打赏
  • 举报
回复
我还以为来踢馆的呢,
这种题目还差远了!
捏造的信仰 2009-08-11
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 wuaner 的回复:]
太感谢大家了,尤其是大虫兄,用他的热心让广大坛友感受到了csdn的温暖!

千言万语汇成一句话:谢谢!
[/Quote]
不要这么客气了,结贴就行。
Wuaner 2009-08-11
  • 打赏
  • 举报
回复
太感谢大家了,尤其是大虫兄,用他的热心让广大坛友感受到了csdn的温暖!

千言万语汇成一句话:谢谢!
xujian2009 2009-08-11
  • 打赏
  • 举报
回复
学习了,果然是高手如云。
hzq237 2009-08-11
  • 打赏
  • 举报
回复
学习了!
flyinghawl 2009-08-11
  • 打赏
  • 举报
回复
hehe,大家很热心啊
sahala3293 2009-08-11
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 bigbug9002 的回复:]
变成蝴蝶就不来了。
[/Quote]
bigbug9002 2009-08-11
  • 打赏
  • 举报
回复
变成蝴蝶就不来了。
  • 打赏
  • 举报
回复
嘿嘿

毛毛虫最近果然活跃

public class Test {

public static void main(String[] args) {
int[][] result1=getMatrix(4,4);
int[][] result2=getMatrix(5,5);
System.out.println("4X4");
printMatrix(result1);
System.out.println("5X5");
printMatrix(result2);

}
public static int[][] getMatrix(int rowNum,int columnNum){
int[][] result=new int[rowNum][columnNum];
int num=0;
int start=0;
int snf=0;
if((columnNum&1)==1){
start=columnNum/2;
}else{
start=columnNum/2-1;
}
for(int i=rowNum-1;i>=0;i--){
int index=start;
if((columnNum&1)==1){
snf=-1;
}else{
snf=1;
}
for(int j=0;j<columnNum;j++){
result[i][index]=num++;
index=index+(j+1)*snf;
snf*=-1;
}
}
return result;
}
public static void printMatrix(int[][] result){
for(int i=0;i<result.length;i++){
for(int j=0;j<result[i].length;j++){
System.out.printf("%5d",result[i][j]);
}
System.out.println();
}
}

}
hujun_zero 2009-08-11
  • 打赏
  • 举报
回复
好~~~
弘石 2009-08-11
  • 打赏
  • 举报
回复
bigbug你可真有耐心,这又是什么问题啊?还以为由多难呢
bigbug9002 2009-08-11
  • 打赏
  • 举报
回复
哦,那个简单啊:

import java.util.*;
public class Test {

public static void main(String[] args) {
int[] result1=getList(4,5);
int[] result2=getList(4,4);
System.out.println(Arrays.toString(result1));
System.out.println(Arrays.toString(result2));


}
public static int[][] getMatrix(int rowNum,int columnNum){
int[][] result=new int[rowNum][columnNum];
int num=0;
int start=0;
int snf=0;
if((columnNum&1)==1){
start=columnNum/2;
}else{
start=columnNum/2-1;
}
for(int i=rowNum-1;i>=0;i--){
int index=start;
if((columnNum&1)==1){
snf=-1;
}else{
snf=1;
}
for(int j=0;j<columnNum;j++){
result[i][index]=num++;
index=index+(j+1)*snf;
snf*=-1;
}
}
return result;
}
public static int[] getList(int rowNum,int columnNum){
int[][] result=getMatrix( rowNum,columnNum);
int [] resultList=new int[rowNum*columnNum];
int index=0;
for(int i=0;i<result.length;i++){
for(int j=0;j<result[i].length;j++){
resultList[index++]=result[i][j];
}
}
return resultList;
}
public static void printMatrix(int[][] result){
for(int i=0;i<result.length;i++){
for(int j=0;j<result[i].length;j++){
System.out.printf("%5d",result[i][j]);
}
System.out.println();
}
}

}

F:\java>java Test
[18, 16, 15, 17, 19, 13, 11, 10, 12, 14, 8, 6, 5, 7, 9, 3, 1, 0, 2, 4]
[14, 12, 13, 15, 10, 8, 9, 11, 6, 4, 5, 7, 2, 0, 1, 3]

【如鱼饮水】 2009-08-11
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 qq271175778 的回复:]
楼主意思好像不是两个矩阵相乘

      感觉就是把矩阵依次输出
[/Quote]
没注意!!! 看错了
晕死
面壁去.....
【如鱼饮水】 2009-08-11
  • 打赏
  • 举报
回复
楼主意思好像不是两个矩阵相乘

感觉就是把矩阵依次输出
单车程序员 2009-08-11
  • 打赏
  • 举报
回复
【如鱼饮水】 2009-08-11
  • 打赏
  • 举报
回复
[Quote=引用楼主 wuaner 的回复:]
有个有maxRow行,maxColumn列的方阵,总共maxRow*maxColumn个元素,元素值从小到大为0,1,2,3,...maxRow*maxColumn-1。


算法目的:实现元素值的这样一种排序:

情况一:maxColumn为奇数(设maxRow=4,maxColumn=5):

18  16  15  17  19
13  11  10  12  14
8  6  5  7  9
3  1  0  2  4

即最终排序为:18  16  15  17  19 ...  3  1  0  2  4。

情况二:maxColumn为偶数(设maxRow=4,maxColumn=4):

14  12  13  15
10  8  9  11
6    4    5    7
2  0  1  3
即最终排序为:14  12  13  15 ...2  0  1  3。


谢谢大家的支持!!!!


[/Quote]
捏造的信仰 2009-08-11
  • 打赏
  • 举报
回复
public class WabbleMatrix {

private int[][] data;

public WabbleMatrix(int rows, int cols) {
this.data = new int[rows][cols];
build(rows, cols);
}

// 构造数组内容
private void build(int rows, int cols) {
int max = rows * cols - 1; // 要填入的第一个数字
int row_pointer = 0; // 当前行
int[] col_pointers = {-1, cols}; // 当前列(两个指针)
int currentpointer = 0; // 当前使用的列指针(0 或 1)

while (max >= 0) {
currentpointer = getCurrentPointer(col_pointers, currentpointer);
data[row_pointer][col_pointers[currentpointer]] = max;
max--;

// 如果两个列指针相邻就移到下一行,并重新摆正列指针位置
if (touched(col_pointers)) {
row_pointer++;
col_pointers = new int[]{-1, cols};
currentpointer = 0;
}
}
}

// 在两个列指针之间切换,并移动列指针
private int getCurrentPointer(int[] pointers, int currentpointer) {
currentpointer = (currentpointer + 1) % pointers.length;
if (currentpointer == 0) {
pointers[currentpointer] = pointers[currentpointer] + 1;
} else {
pointers[currentpointer] = pointers[currentpointer] - 1;
}
return currentpointer;
}

// 判断两个列指针是否相邻
private boolean touched(int[] pointers) {
return pointers[0] + 1 == pointers[1];
}

// 格式化输出
public String toString() {
String str = "";
for (int[] rows : data) {
for (int item : rows) {
str += String.format("%5d", item);
}
str += "\n";
}
return str;
}

// 程序入口
public static void main(String[] args) {
WabbleMatrix matrix = new WabbleMatrix(4, 5);
System.out.println(matrix);
System.out.println();

matrix = new WabbleMatrix(4, 4);
System.out.println(matrix);
}
}
bigbug9002 2009-08-11
  • 打赏
  • 举报
回复
没有看仔细奇数的情况,下面的代码应该没有问题:

public class Test { 

public static void main(String[] args) {
int[][] result1=getMatrix(4,4);
int[][] result2=getMatrix(5,5);
System.out.println("4X4");
printMatrix(result1);
System.out.println("5X5");
printMatrix(result2);

}
public static int[][] getMatrix(int rowNum,int columnNum){
int[][] result=new int[rowNum][columnNum];
int num=0;
int start=0;
int snf=0;
if((columnNum&1)==1){
start=columnNum/2;
}else{
start=columnNum/2-1;
}
for(int i=rowNum-1;i>=0;i--){
int index=start;
if((columnNum&1)==1){
snf=-1;
}else{
snf=1;
}
for(int j=0;j<columnNum;j++){
result[i][index]=num++;
index=index+(j+1)*snf;
snf*=-1;
}
}
return result;
}
public static void printMatrix(int[][] result){
for(int i=0;i<result.length;i++){
for(int j=0;j<result[i].length;j++){
System.out.printf("%5d",result[i][j]);
}
System.out.println();
}
}

}


F:\java>java Test
4X4
14 12 13 15
10 8 9 11
6 4 5 7
2 0 1 3
5X5
23 21 20 22 24
18 16 15 17 19
13 11 10 12 14
8 6 5 7 9
3 1 0 2 4


bacel5902 2009-08-11
  • 打赏
  • 举报
回复
	public void doCR(int c,int r){
if(c%2==0){//偶数列
for(int i=r;i>0;i--){
int j1=c*i;int j2=c*(i-1);int j3=j1-2;
while(j3>j2){
System.out.print(j3+" ");
j3=j3-2;
}
j3=j2;
System.out.print(j3+" ");
j3++;
while(j3<j1){
System.out.print(j3+" ");
j3=j3+2;
}
System.out.println("");
}

}
else{//奇数列
for(int i=r;i>0;i--){
int j1=c*i;int j2=c*(i-1);int j3=j1-2;
while(j3>j2){
System.out.print(j3+" ");
j3=j3-2;
}
j3=j2;
while(j3<j1){
System.out.print(j3+" ");
j3=j3+2;
}
System.out.println("");
}
}
}
加载更多回复(9)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧