C语言网

 找回密码
 加入社区!

QQ登录

只需一步,快速开始

查看: 838|回复: 5

一个画图程序 要求打印出 [复制链接]

Rank: 1

主题
0
帖子
11
C币
24 枚
在线时间
0 小时
发表于 2010-3-2 19:29:29 |显示全部楼层
分享到:
一个画图程序 要求打印出
int i=5;
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

int i=6
1  2  3  4  5   6
20 21 22 23 24  7
19 32 33 34 25  8
18 31 36 35 26  9
17 30 29 28 27 10
16 15 14 13 12 11

Rank: 16Rank: 16Rank: 16Rank: 16

主题
23
帖子
278
C币
1031 枚
在线时间
303 小时
发表于 2010-3-2 19:31:20 |显示全部楼层
class snakePrint {
        static int length = 7;
        static int value = 1;
        static int[][] snake = new int[length][length];
        static Direction lastDirection = Direction.Right;

        static enum Direction {
                Right, Down, Left, Up;
        }

        public static void initialArray() {
                int row = 0, line = 0;
                for (int c = 0; c < length * length; c++) {
                        snake[row][line] = value;
                        lastDirection = findDirection(row, line);
                        switch (lastDirection) {
                                case Right:
                                        line++;
                                        break;
                                case Down:
                                        row++;
                                        break;
                                case Left:
                                        line--;
                                        break;
                                case Up:
                                        row--;
                                        break;
                                default:
                                        System.out.println("error");
                        }
                        value++;
                }
        }

        static Direction findDirection(int row, int line) {
                Direction direction = lastDirection;
                switch (direction) {
                        case Right: {
                                if ((line == length - 1) || (snake[row][line + 1] != 0))
                                        direction = direction.Down;
                                break;
                        }
                        case Down: {
                                if ((row == length - 1) || (snake[row + 1][line] != 0))
                                        direction = direction.Left;
                                break;
                        }
                        case Left: {
                                if ((line == 0) || (snake[row][line - 1] != 0))
                                        direction = direction.Up;
                                break;
                        }
                        case Up: {
                                if (snake[row - 1][line] != 0)
                                        direction = direction.Right;
                                break;
                        }
                }
                return direction;
        }

        public static void main(String[] args) {
                initialArray();

                // display.....
                for (int i = 0; i < length; i++) {
                        for (int j = 0; j < length; j++) {
                                System.out.print(snake[i][j] + "  ");
                        }
                        System.out.println();
                }
        }
}

Rank: 16Rank: 16Rank: 16Rank: 16

主题
23
帖子
278
C币
1031 枚
在线时间
303 小时
发表于 2010-3-2 19:32:09 |显示全部楼层
public static void main(String args[]){
          int N=5;
          int a[][]=new int[N][N];
          int i=0,j=0;
          int count=1;
          for(i=0;i<N;i++){
                  for(j=0;j<N;j++){
                          a[i][j]=0;
                  }
          }
          i=0;
          j=0;
      for(int k=0;k<=N/2;k++){
              i=k;
              j=k;
                  for(i=k;i<N-k;i++){
                          a[j][i]=count;
                          count++;
                  }
                  i=N-k-1;
                  for(j=k+1;j<N-k;j++){
                          a[j][i]=count;
                          count++;
                  }
                  j=N-k-1;
                  for(i=N-k-2;i>=k;i--){
                          a[j][i]=count;
                          count++;
                  }
                  i=k;
                  for(j=N-k-2;j>=1+k;j--){
                          a[j][i]=count;
                          count++;
                  }
      }
                 
          for(i=0;i<N;i++){
                  for(j=0;j<N;j++){
                     System.out.print(a[i][j]+" ");
                  }
                  System.out.println();
          }
}

点评

admin  一年半前的帖子就将就着看吧~ :)  发表于 2011-10-17 10:43:51
々曲散〃人终々  - -管理。你不是说发代码的时候要把代码圈起来吗?  发表于 2011-10-17 08:41:45

Rank: 1

主题
4
帖子
11
C币
19 枚
在线时间
2 小时
发表于 2010-11-12 12:51:52 |显示全部楼层
int i=5;
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

int i=6
1  2  3  4  5   6
20 21 22 23 24  7
19 32 33 34 25  8
18 31 36 35 26  9
17 30 29 28 27 10
16 15 14 13 12 11


楼主能否解释一下这个画图程序的作用
            新来的,学习C语言,各位大哥请多多指教

Rank: 1

主题
0
帖子
3
C币
15 枚
在线时间
0 小时
发表于 2011-9-11 20:54:53 |显示全部楼层
???

Rank: 1

主题
0
帖子
5
C币
20 枚
在线时间
1 小时
发表于 2011-10-16 22:49:30 |显示全部楼层
楼主的程序似乎有点点小问题啊。新手路过。。。。
您需要登录后才可以回帖 登录 | 加入社区!

C语言 ( 粤ICP备11042647号-2 )

GMT+8, 2012-5-20 17:31

©2009-2011 cyuyan.com.cn

回顶部