| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 621 人关注过本帖
标题:求助制作方阵思路
只看楼主 加入收藏
whys
Rank: 1
等 级:新手上路
帖 子:50
专家分:8
注 册:2009-10-27
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
求助制作方阵思路
当n=4时,输出如下形式方阵:
1   2   6   7
3   5   8   13
4   9   12  14
10  11  15  16

我看不出规律,求助
搜索更多相关主题的帖子: 方阵 思路 制作 
2010-09-02 12:00
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:10 
这个叫蛇形方阵
思路: 二维数组

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2010-09-02 12:05
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:5 
程序代码:
public class Dr {
    private int[][] num;

    private void print(int n) {
        num = new int[n][n];
        int count = 1;
        int row = 0, col = 0;
        int endRow = n - 1, endCol = n - 1, endCount = n * n;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < i + 1; j++) {
                num[row][col] = count;
                count++;

                if (i % 2 == 0) {
                    row--;
                    col++;
                } else {
                    row++;
                    col--;
                }

                if (row < 0) {
                    row++;
                    col--;
                } else if (col < 0) {
                    col++;
                    row--;
                }
            }
            if (row == 0) {
                col++;
            } else if (col == 0) {
                row++;
            }

            for (int j = 0; j < i + 1; j++) {
                if (i == n - 1)
                    break;
                num[endRow][endCol] = endCount;
                endCount--;

                if (i % 2 != 0) {
                    endRow--;
                    endCol++;
                } else {
                    endRow++;
                    endCol--;
                }
                if (endCol > n - 1) {
                    endCol--;
                    endRow++;
                } else if (endRow > n - 1) {
                    endCol++;
                    endRow--;
                }
            }
            if (endRow == n - 1) {
                endCol--;
            } else if (endCol == n - 1) {
                endRow--;
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.printf("%4d", num[i][j]);
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        Dr d = new Dr();
        d.print(4);
        System.out.println();
        d.print(7);
    }
}
2010-09-02 16:59
虾B写
Rank: 8Rank: 8
来 自:湖北
等 级:蝙蝠侠
威 望:3
帖 子:395
专家分:922
注 册:2009-10-1
收藏
得分:5 
有规律的

程序代码:
public class aa{

    private static int s[][];

    public static void main(String as[]){
        int n1=0,n2=0,nn=0,o=4;
        s=new int[o][o];
        for(int u=0;u<o;u++){    
            for(int i=0,j=u;i<=u;i++,j--){
                if(u%2==0){n1=j;n2=i;}else{n1=i;n2=j;}
                nn++;
                s[n1][n2]=nn;
                if(u!=o-1){s[o-1-n1][o-1-n2]=o*o+1-nn;}
            }

        }
        

        for(int i=0;i<o;i++){
            String ss="";
            for(int ii=0;ii<o;ii++){
                ss+=s[i][ii]+" ";
        
            }
            System.out.println(ss);
        }

    }


}

白娘故意下雨骗许仙的伞。祝英台十八里相送时装疯卖傻调戏梁山伯。七仙女挡住了董永的去路。牛郎趁织女洗澡时拿走了她的衣服。。。这些故事告诉我们;伟大爱情的开始,总归的有一个要先耍流氓!
2010-09-02 21:17
快速回复:求助制作方阵思路
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014846 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved