郁闷的,简单的一个题目完全可以做对的,结果犯了好几个低级错误
笔试的时候我是按第一种方法的思路做的,结束后我重新整理了一下,写了两个
感觉还写的不好,再怎么改比较好呢
import java.util.Set;
import java.util.TreeSet;public class MineUseSet {
static int WIDTH = 11;
static int HIGH = 12;
static int NUM = 13;

public static void main(String[] args) {
MineUseSet.showMine(WIDTH, HIGH, NUM);
}

public static void showMine(int weight, int high, int num) {
int[][] a = new int[high][weight];
int k = 0;
int random = 0;
Set set = new TreeSet();
int total = weight * high; do {
random = (int)(total * Math.random() + 1);
set.add(random);
} while(set.size() < num);
for(int i=0; i<high; i++) {
for(int j=0; j<weight; j++) {
a[i][j] = k++;
}
System.out.println();
}
for(int i=0; i<high; i++) {
for(int j=0; j<weight; j++) {
if(!set.contains(a[i][j]))
a[i][j] = 1;  // 1 indicate a mine;
else
a[i][j] = 0;      //0 indicate nothing;
}
}
for(int i=0; i<high; i++) {
for(int j=0; j<weight; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}

}}结果:
1 1 1 1 1 1 1 1 1 1 0 
1 1 1 1 0 1 0 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 
1 0 1 0 1 1 1 1 1 1 0 
1 1 1 1 1 1 1 1 1 1 1 
0 1 1 1 1 0 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 0 1 1 
1 0 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 0 1 1 
package test;public class TestMine {
//H indicate high
//W indicate width
//NUM indicate the number of mine
static int H = 6;
static int W = 7;
static int NUM = 3;
public static void main(String[] args) {
TestMine.showMine(H, W, NUM);
}

public static void showMine(int h, int w, int num) {
String[][] b= new String[h][w];
for(int i=0; i<h; i++) {
for(int j=0; j<w; j++) {
b[i][j] = "0";
}
}
int m = 0;
int n = 0;
//      random generate mine in the array
// "*" indicate a mine
for(int count = 0; count < num; count++) {
m = (int)(h * Math.random());
n = (int)(w * Math.random());
if(b[m][n].equals("0")) {
b[m][n] = "*";
} else
count--;
}
// print the result
for(int i=0; i<h; i++){
for(int j=0; j<w; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
}}
结果:
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
* 0 0 * 0 0 0 
0 0 0 0 0 0 * 
0 0 0 0 0 0 0 

解决方案 »

  1.   


    /*
     * Test.java
     *
     * Created on 2008年2月20日, 上午10:41
     *
     *
     */package samck;
    /**
     *
     * @author [email protected]
     */
    public class Test{
        static int H = 6;
        static int W = 7;
        static int NUM = 10;
        
        public static void main(String[] args) {
            int[][] ct = new int[H][W];
            for(int i=0;i<NUM;i++){ //填充随机
                int index = (int)( H*W* Math.random());
                ct[index/W][index%W] = 1;
            }
            
            
            //显示数组
            for(int i=0;i<ct.length;i++){
                for(int j=0;j<ct[0].length;j++){
                    System.out.print(ct[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
      

  2.   

    楼上的写的挺简单的,不过漏掉了重复的判断
    一个位置上可不能放多个雷呀/*
     * Test.java
     *
     * Created on 2008年2月20日, 上午10:41
     *
     *
     */package samck;
    /**
     *
     * @author [email protected]
     */
    public class Test{
        static int H = 6;
        static int W = 7;
        static int NUM = 10;
        
        public static void main(String[] args) {
            int[][] ct = new int[H][W];
            for(int i=0;i<NUM;i++){ //填充随机
                int index = (int)( H*W* Math.random());
                if(ct[index/W][index%W] = 1)
                     i--;
                else
                    ct[index/W][index%W] = 1 
            }
            
            
            //显示数组
            for(int i=0;i<ct.length;i++){
                for(int j=0;j<ct[0].length;j++){
                    System.out.print(ct[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
      

  3.   


    /*
     * Test.java
     *
     * Created on 2008年2月20日, 上午10:41
     *
     *
     */package samck;import java.util.HashSet;
    import java.util.Set;
    /**
     *
     * @author [email protected]
     */
    public class Test{
        static int H = 6;
        static int W = 7;
        static int NUM = 10;
        
        public static void main(String[] args) {
            int[][] ct = new int[H][W];
            Set set = new HashSet();
            while(set.size()<NUM){
                int index =  (int)(H*W* Math.random());
                set.add(index);
                ct[index/W][index%W] = 1;
            }        //显示数组
            for(int i=0;i<ct.length;i++){
                for(int j=0;j<ct[0].length;j++){
                    System.out.print(ct[i][j] + " ");
                }
                System.out.println();
            }
            set = null;
        }
    }
      

  4.   


    static public int[][] init(int size, int num) {
    if (size < 0 || num < 0 || num >= size * size) {
    throw new UnsupportedOperationException();
    } int[][] cc = new int[size][size];
    int count = 0;
    ArrayList<Integer> list = new ArrayList<Integer>();
    Random random = new Random(size * size);
    while (count < num) {
    int i = random.nextInt(size * size);
    if (!list.contains(i)) {
    list.add(i);
    cc[i / size][i % size] = 1;
    count++;
    }
    } return cc;
    }0 0 0 0 0 0 0 0 0 0 
    0 0 0 1 0 1 0 0 0 0 
    0 0 1 1 0 0 0 0 0 0 
    0 0 0 0 0 0 1 0 0 0 
    0 0 0 0 0 0 0 0 0 0 
    1 0 0 0 0 0 0 0 0 0 
    0 0 0 0 0 0 1 0 0 0 
    0 0 0 0 1 0 0 0 0 0 
    0 0 0 0 0 0 0 0 1 0 
    0 1 0 0 0 0 0 0 0 0 
      

  5.   

    5楼的方法参数设的比较特殊
    还有ArrayList的元素是可以重复的
    也就会出现一个位置放多个雷的情况了。。惨,我昨天笔试的时候写了个
    Set set = new TreeSet();
    回去重写程序才想起来,疯掉了
      

  6.   

    ArrayList是可以保证不重复情况的,楼主可以自己试一试,呵呵,建议了解下
      

  7.   

    package test;import java.util.Random;/**
     * 
     * @author 老紫竹 www.java2000.net
     *
     */
    public class MineTest {  public static void main(String[] args) {
        showMine(11,12,13);
      }
      public static void showMine(int width, int high, int total) {
        int[][] place = new int[high][width];
        Random r = new Random();
        int w, h;
        while (total > 0) {
          w = r.nextInt(width - 1);
          h = r.nextInt(high - 1);
          if (place[h][w] == 0) {
            place[h][w] = 1;
            total--;
          }
        }
        for (int i = 0; i < high; i++) {
          for (int j = 0; j < width; j++) {
            System.out.print(place[i][j] + " ");
          }
          System.out.println();
        }
      }
    }
    我的解决方案! 我想还可以吧!
      

  8.   

    更正一下
          w = r.nextInt(width);
          h = r.nextInt(high);
      

  9.   


    import java.util.ArrayList;
    import java.util.List;public class TestArrayList { public static void main(String[] args) {
    List a = new ArrayList();
    for(int i=0; i< 5; i++)
    a.add(11);
    System.out.print(a);
    }
    }result:[11, 11, 11, 11, 11]
    如果不重复结果不是该如下面的吗?
    [11]
      

  10.   

    来个另类点的 哈哈 想了我好长时间public class MineTest {
    public static void main(String[] args){
    int[][] array=mineConstruct(11,12,13);
    for(int[] i:array){
    for(int j:i){
    System.out.printf("%2d",j);
    }
    System.out.println();
    }

    }
    public static int[][] mineConstruct(int hSize,int vSize,int mineNum){
    int[][] mineArray=new int[hSize][vSize];
    int totalNum=hSize*vSize;
    for(int i=0;i<hSize;i++)
    for(int j=0;j<vSize;j++){
    if(totalNum*Math.random()<(mineNum)){
    mineArray[i][j]=1;
    mineNum--;
    totalNum--;
    }
    else{
    mineArray[i][j]=0;
    totalNum--;
    }
    }
    return mineArray;
    }
    }output:
     0 0 0 0 0 0 0 0 0 1 0 1
     0 0 1 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 1 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 1 0
     0 0 0 1 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 1 0 0 0 1
     0 0 0 0 0 0 0 0 0 0 1 0
     0 0 0 0 0 0 0 0 1 0 1 0
     0 0 1 1 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 1 0 0
     0 1 0 0 0 1 0 1 1 1 0 0
     0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 1 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 1 1
     0 0 0 0 0 0 0 1 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0
     0 0 1 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0似乎随机性还不错
      

  11.   

    这样修改下就可以避免产生随机数的重复现象!
    public class Test2
    {
    static int WIDTH = 11; static int HIGH = 12; static int NUM = 13; public static void main(String[] args)
    {
    Test2.showMine(WIDTH, HIGH, NUM);
    } public static void showMine(int weight, int high, int num)
    {
    int[][] a = new int[high][weight];
    int k = 0;
    int random = 0;
    Set set = new TreeSet();
    int total = weight * high;
    int[] temp = new int[num];
    ArrayList<Integer> list = new ArrayList<Integer>();
    do
    {
    random = (int) (total * Math.random() + 1);
    if (!list.contains(random))
    {
    list.add(random);
    set.add(random);
    System.out.println(random);
    }

    } while (set.size() < num); for (int i = 0; i < high; i++)
    {
    for (int j = 0; j < weight; j++)
    {
    a[i][j] = k++;
    }
    System.out.println();
    } for (int i = 0; i < high; i++)
    {
    for (int j = 0; j < weight; j++)
    {
    if (!set.contains(a[i][j]))
    a[i][j] = 1; // 1 indicate a mine;
    else
    a[i][j] = 0; // 0 indicate nothing;
    }
    } for (int i = 0; i < high; i++)
    {
    for (int j = 0; j < weight; j++)
    {
    System.out.print(a[i][j] + " ");
    }
    System.out.println();
    } }}
      

  12.   

    to 12楼
        不是很另类呀想了下,好像还没有考虑雷的数量应该不大于二维数组的元素的个数
    应该还要加下面的代码total = total >(W * H)?:(W * H) : total;
      

  13.   

    package samck;public class chees{
    private   int   WIDTH; 
    private   int   HIGH; 
    synchronized private   int   NUM; 
    private   int[][] place;
    public Chees(int high,int width,int num)
    {   int[][] place = new int[high][width];
        WIDTH = width;
        NUM = num;
        HIGH = high;
    }   public static void main(String[] args) {
        Chees ch = new Chees(4,5,8);
        ch.getChees();
        ch.listChees();
        }
    synchronized public void getChees()
       {
       new Tread() {
          public void run
          {
          while(1)
          {
          if ( place[WIDTH *HIGH *Math.random()] == 8 )
          else 
          {
             synchronized(this) {
               if(NUM > 0)
               {
                  place[WIDTH *HIGH *Math.random()] = 8;
                  NUM --;
               }else{
                  break;
               }
             }
          }
          }
          Tread.yield;
       }.start;
       }public void listChees()
    {
        //显示
    }
    }肯定有许多问题,刚学并发又没有地方运行下,就当是个方法了。
      

  14.   

    ArrayList允许重复 而HashSet不行
    所以我用while(set.size()<10)来保证取出10个雷
      

  15.   

    public class Shaolei { int[][] lei;
    int x;
    int y;

    public void print(){
    for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
    System.out.print(lei[i][j]);
    }
    System.out.println();
    }
    } public Shaolei(int x, int y, int n) {
    lei= new int[x][y];
    this.x = x;
    this.y = y;
    int b = 0;
    for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
    lei[i][j] = 1;
    }
    }
    int k = 0;
    while(k<n){
    int index = (int)(x*y*Math.random());
    if(lei[index%x][index/x] != 0){
    lei[index%x][index/x] = 0;
    k++;
    }
    }

    } public static void main(String[] args) {
    // TODO Auto-generated method stub
    Shaolei shaolei = new Shaolei(8, 11, 12);
    shaolei.print();
    }}直接用一个变量记录雷数,达到跳出循环
      

  16.   

    to 18楼,你真的看明白我是怎么用ArrayList的吗?
      

  17.   

    to 24:
    你用了ArrayList.contains()方法来避免重复 这也可以 但就没HashSet效率高
      

  18.   

    顶19楼,虽然这随机分布的效果不太理想,待改进吧。
    public class ShaoLei {  int[][] lei;
      int x;
      int y;
      java.util.Random random;
      public void print() {
        for (int i = 0; i < x; i++) {
          for (int j = 0; j < y; j++) {
            if (lei[i][j] == -1)
              System.out.print("*");
            else
              System.out.print(lei[i][j]);
          }
          System.out.println();
        }
      }  public ShaoLei(int x, int y, int n) {
        lei = new int[x][y];
        this.x = x;
        this.y = y;
        int b = 0;
        for (int i = 0; i < x; i++) {
          for (int j = 0; j < y; j++) {
            lei[i][j] = 0;
          }
        }
        int k = 0;
        while (k < n) {
          int index = java.lang.Math.abs(random.nextInt() % (x * y));
          if (lei[index % x][index / x] != -1) {
            lei[index % x][index / x] = -1;
            k++;
          }
        }
        for (int i = 0; i < x; i++) {
              for (int j = 0; j < y; j++) {
                int count = 0;
        
                // 当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
                if (lei[i][j] != 20) {
                  if ( (i - 1 >= 0) && (j - 1 >= 0)) {
                    if (lei[i - 1][j - 1] == 20) {
                      count += 1; //左上方
                    }
                  }
        
                  if ( (i - 1 >= 0)) {
                    if (lei[i - 1][j] == 20) {
                      count += 1; // 上方
                    }
                  }
        
                  if ( (i - 1 >= 0) && (j + 1 < y)) {
                    if (lei[i - 1][j + 1] == 20) {
                      count += 1; // 右上方                }
                  }
        
                  if ( (j - 1 >= 0)) {
                    if (lei[i][j - 1] == 20) {
                      count += 1; // 左边
                    }
                  }
        
                  if ( (i >= 0) && (j + 1 < y)) {
                    if (lei[i][j + 1] == 20) {
                      count += 1; // 右边
                    }
                  }
        
                  if ( (j - 1 >= 0) && (i + 1 < x)) {
                    if (lei[i + 1][j - 1] == 20) {
                      count += 1; // 左下
                    }
                  }
        
                  if ( (i + 1 < x)) {
                    if (lei[i + 1][j] == 20) {
                      count += 1; // 下
                    }
                  }
        
                  if ( (j + 1 < y) && (i + 1 < x)) {
                    if (lei[i + 1][j + 1] == 20) {
                      count += 1; // 右下
                    }
                  }
                  lei[i][j] = count;
                }
              }
            }  }  public static void main(String[] args) {
        // TODO Auto-generated method stub
        ShaoLei shaolei = new ShaoLei(10, 10, 20);
        shaolei.print();
      }}
      

  19.   

    public class radom {
        static int WIDTH = 9;//排雷区间的宽度
        static int HIGH = 9;//排雷区间的高度
        static int NUM = 13;//雷的个数
        
        public static void showMine(int weight, int high, int num) {
            String[][] a = new String[high][weight];//定义2维数组
            for(int i=0; i<high; i++) {
                for(int j=0; j<weight; j++) {                
                        a[i][j] = "o"; 
                }
            }
            for(int account=0;account<num;account++){
              do {
                  int h = (int) (high*Math.random());
                  int   w = (int) (weight*Math.random());
               if(a[h][w]=="o"){
                      a[h][w]="@";
                      break;}
              }while(true);
            }
            for(int i=0; i<high; i++) {
                for(int j=0; j<weight; j++) {
                    System.out.print(a[i][j] + " ");
                }
                System.out.println();
            }    
            
        }
    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    showMine(WIDTH,HIGH,NUM);
    }
    参照各位的思路写了,不要见笑啊。
      

  20.   

     2 # 2 # 1 0 2 # # 1
     # 2 2 1 1 1 4 # 5 2
     1 1 0 0 0 1 # # # 1
     0 1 2 2 2 2 3 4 3 2
     1 3 # # 4 # 3 2 # 2
     1 # # # 5 # 5 # 5 #
     1 3 5 6 # 6 # # # 2
     1 2 # # # # # 6 3 1
     # 3 2 3 4 # # # 3 2
     # 2 0 0 1 2 3 3 # #public class MineTest {
        public static void main(String[] args) {
            Mine mine = new Mine(10, 10, 35);        
            char[][] mineArray = mine.getMineArray();
            Mine.printMineArray(mineArray);
        }
    }import java.util.Random;public class Mine {
        /**
         * 雷阵列
          */
        private char[][] mineArray;
        
        /**
         * 雷的总数
          */
        private int total;
        
        /**
         * 有雷时的标记
          */
        public final static char MINE = '#';
        
        /**
         * 周围无雷时的标记
          */
        public final static char BLANK = '0';        public Mine(int height, int width, int total) {
            if(height * width < total) {
                throw new IllegalArgumentException(total + " is too many, total " +
                        "must be less than or equal to " + (height * width));
            }
            this.total = total;
            init(height, width);
        }    public int getTotal() {
            return total;
        }
        
        public char[][] getMineArray() {
            return this.mineArray;
        }
        
        /**
         * 输出雷阵列的工具方法
          * @param mineArray 雷阵列
          */
        public static void printMineArray(char[][] mineArray) {
            for(int i = 0; i < mineArray.length; i++) {
                for(int j = 0; j < mineArray[i].length; j++) {
                    System.out.printf("%2c", mineArray[i][j]);
                }
                System.out.println();
            } 
        }
        
        /**
         * 初始化雷阵列
          * @param height 阵列的行数
          * @param width 阵列的列数
          */
        private void init(int height, int width) {
            mineArray = new char[height][width];        
            Random r = new Random();
            int y = 0, x = 0, t = 0;
            
            // 置雷
             while(t < total) {
                y = r.nextInt(height);
                x = r.nextInt(width);
                if (mineArray[y][x] != MINE) {
                    pubMine(y, x);
                    t++;
                }
            }
            
            // 将空白处置为周围无雷标记
             for (int i = 0, m = mineArray.length; i < m; i++) {
                for (int j = 0, n = mineArray[i].length; j < n; j++) {
                    if(mineArray[i][j] == 0) {
                        mineArray[i][j] = BLANK;
                    }
                }
            }
        }
        
        /**
         * 置雷,并在九宫周围单元格中置上单元格周围雷的数量
          * @param y 纵坐标
          * @param x 横坐标
          */
        private void pubMine(int y, int x) {
            // 扫描整个九宫
             for(int i = y - 1; i <= y + 1; i++) {
                // 行值在范围之外时忽略
                  if(i < 0 || i > mineArray.length - 1) {
                    continue;
                }
                
                for(int j = x - 1; j <= x + 1; j++) {
                    // 列值在范围之外时忽略
                      if(j < 0 || j > mineArray.length - 1 ) {
                        continue;
                    }
                    // 在九宫中心位置时置雷
                      if(i == y && j == x) {
                        mineArray[i][j] = MINE;
                    }
                    // 如果周围单元格为雷时忽略
                      if(mineArray[i][j] == MINE) {
                        continue;
                    }
                    
                    if(mineArray[i][j] == 0) {
                        // 周围单元格为空时,标记为“1”
                           mineArray[i][j] = '1';
                    }else{
                        // 周围单元格不为空时,标记为当前数值增加1
                        mineArray[i][j] += 1;
                    }                
                }
            }
        }
    }
      

  21.   

    你们都喜欢设置3个参数呢?我感觉2个更好.特别是第三个参数,如果一直手快设置成大于前2个参数的积,就会有可能出异常import java.util.HashSet;
    import java.util.Set;public class TestMine {
    //H indicate high
    //W indicate width
        static int H = 10;
        static int W = 10;
        public static void main(String[] args) {
            TestMine.showMine(H, W);
        }
        
        public static void showMine(int h, int w) {
            int[][] b= new int[h][w];
            int num=0;
            num=(int)(h*w*Math.random());
    //         "*" indicate a mine
            Set set = new HashSet();
            while(set.size()<num){
                int index =  (int)(h*w* Math.random());
                set.add(index);
                b[index/h][index%w] = 1;
            }
    //         print the result    
            for(int[] bb:b){
                for(int bbb :bb) {
                    System.out.print(bbb + " ");                
                }
                System.out.println();
            }
        } }