黑马程序员技术交流社区

标题: 自认为最简单的内螺旋数组打印代码 源码源码源码 [打印本页]

作者: 痕迹ql    时间: 2016-4-19 22:35
标题: 自认为最简单的内螺旋数组打印代码 源码源码源码
import java.util.Scanner;
class Helix {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入一个数字:");
                int n = sc.nextInt();
                System.out.println("请输入一个数字:");
                int a = sc.nextInt();
                int[][] arr = new int [n][n];
                int x,y,m;
                for (x=0,m=0;m<n/2+1;m++ ){
                        for (y=x;x<n-m ;x++ ){
                                arr[y][x] = a++;
                        }
                        x--;
                        for (y=y+1;y<n-m ;y++ ){
                                arr[y][x] = a++;
                        }
                        y--;
                        for (x=x-1;x>=m ;x-- ){
                                arr[y][x] = a++;
                        }
                        x++;
                        for (y=y-1;y>m ;y-- ){
                                arr[y][x] = a++;
                        }
                        x++;
                }
                for (int i = 0;i < n ;i++ ){
                        for (int j = 0;j < n ;j++ ){
                                System.out.print(arr[i][j]+"\t");
                        }
                        System.out.println();
                        System.out.println();
                }
        }
}
以上是源码,有兴趣的可以看看     觉得可以就赏点黑马币呗{:2_33:}

(43{12FS}NK$CEQ$XEQVXJF.png (3.01 KB, 下载次数: 0)

运行效果图

运行效果图

作者: zhuzhibo    时间: 2016-4-19 22:55
加油加油!!
作者: 痕迹ql    时间: 2016-4-19 23:07
zhuzhibo 发表于 2016-4-19 22:55
加油加油!!

兄弟  给点黑马币鼓励鼓励年轻人呗{:2_33:}
作者: Warts    时间: 2016-4-19 23:11
加油加油!
作者: hmcxypzq    时间: 2016-4-19 23:26
赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞
作者: 无尽落寞    时间: 2016-4-20 00:10
黑马币这种东西,还得自己赚
作者: yaolv7    时间: 2016-4-20 00:23
  1. import javax.swing.JOptionPane;

  2. public class NeiLuoXuan01 {
  3.     public static void main(String[] args) {
  4.             String s = JOptionPane.showInputDialog("请输入行列数:");
  5.                 int count = Integer.parseInt(s);
  6.         int max = count * count;
  7.         int[][] data = new int[count][count];
  8.         int x = -1, y = 0, cur = 0;
  9.          
  10.         while (cur < max) {
  11.             for (; x + 1 < count && data[x + 1][y] == 0; data[y][++x] = ++cur);
  12.             for (; y + 1 < count && data[x][y + 1] == 0; data[++y][x] = ++cur);
  13.             for (; x - 1 >= 0 && data[y][x - 1] == 0; data[y][--x] = ++cur);
  14.             for (; y - 1 >= 0 && data[y - 1][x] == 0; data[--y][x] = ++cur);
  15.         }

  16.         for (int i = 0; i < count; ++i) {
  17.             for (int j = 0; j < data[i].length; ++j) {
  18.                 System.out.printf("%-3d ", data[i][j]);
  19.             }
  20.             System.out.println();
  21.         }
  22.     }
  23. }
复制代码


来,给你看个很屌的,不是我写的,我也看不懂,有啥问题也别问我呐
作者: 痕迹ql    时间: 2016-4-20 00:31
yaolv7 发表于 2016-4-20 00:23
来,给你看个很屌的,不是我写的,我也看不懂,有啥问题也别问我呐

原理一样啊   只是他这个把for循环写成了行语句而已
作者: yaolv7    时间: 2016-4-20 00:36
  1. import javax.swing.JOptionPane;

  2. /**
  3. * 逆时针螺旋数组
  4. */
  5. public class NiShiZhenLuoXuan {//逆时针螺旋
  6.         private int[][] numArray;
  7.         int x=0;
  8.         int y=0;
  9.         public static void main(String[] args) {
  10.                 NiShiZhenLuoXuan loopACW = new NiShiZhenLuoXuan();
  11.                 loopACW.createArray();
  12.                 loopACW.printToConsole();
  13.                
  14.         }
  15.         private void createArray() {
  16.                 int i=0,j=0;//数组下标
  17.                 int c=0;//层数,从0开始,每一层都是一个正方形(或长方形),总共有四条边,每一个边的角上,就是拐点
  18.                 int d=0;//0,表示第一条边,1,表示第二条边,2,表示第三条,3,表示第四条
  19.                 String s = JOptionPane.showInputDialog("请输入行列数,逗号隔开");
  20.                 String[] a = s.split(",");
  21.                 x = Integer.parseInt(a[0]);
  22.                 y = Integer.parseInt(a[1]);
  23.                 numArray = new int[x][y];
  24.             int maxValue = x*y;
  25.             
  26.             for(int k=1;k<=maxValue;k++){
  27.                     if(d==0){
  28.                             if(i==x-c-1){//第一个拐点
  29.                                     d=1;
  30.                             }else{
  31.                                     numArray[i][j]=k;
  32.                                     i++;
  33.                                     continue;
  34.                             }
  35.                     }
  36.                     if(d==1){
  37.                             if(j==y-c-1){//第二个拐点
  38.                                     d=2;
  39.                             }else{
  40.                                     numArray[i][j]=k;
  41.                                     j++;
  42.                                     continue;
  43.                             }
  44.                     }
  45.                     if(d==2){
  46.                             if(i==c){//第三个拐点
  47.                                     d=3;
  48.                             }else{
  49.                                     numArray[i][j]=k;
  50.                                     i--;
  51.                                     continue;
  52.                             }
  53.                     }
  54.                     if(d==3){
  55.                             if(j==c+1){//第四个拐点的处理比较特殊
  56.                                     numArray[i][j]=k;
  57.                                     d=0;
  58.                                     c++;
  59.                                     i++;
  60.                                     continue;
  61.                             }else{
  62.                                     numArray[i][j]=k;
  63.                                     j--;
  64.                                     continue;
  65.                             }
  66.                     }         
  67.             }
  68.                
  69.         }
  70.        
  71.         private void printToConsole() {
  72.                 String space = "\t";//原来的 String space = "\t";
  73.                 for(int i=0;i<x;i++){
  74.                         for(int j=0;j<y;j++){
  75.                                 /*if(numArray[i][j]<10){
  76.                                         space="  ";
  77.                                 }else{
  78.                                         space=" ";
  79.                                 }*/
  80.                                 System.out.print(numArray[i][j]+space);
  81.                         }
  82.                         System.out.println();//换行
  83.                 }
  84.                
  85.         }
  86. }
复制代码


不一样的也行,这个行数和列数可以自定
作者: liudh1    时间: 2016-4-20 09:55
给力!!!!赞
作者: 痕迹ql    时间: 2016-4-20 13:15
yaolv7 发表于 2016-4-20 00:36
不一样的也行,这个行数和列数可以自定

我的行数列数也是自定  哥们
作者: yaolv7    时间: 2016-4-21 20:53
痕迹ql 发表于 2016-4-20 13:15
我的行数列数也是自定  哥们

你确定你的是行列数自定么?




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2