郁闷的,简单的一个题目完全可以做对的,结果犯了好几个低级错误
笔试的时候我是按第一种方法的思路做的,结束后我重新整理了一下,写了两个
感觉还写的不好,再怎么改比较好呢
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
笔试的时候我是按第一种方法的思路做的,结束后我重新整理了一下,写了两个
感觉还写的不好,再怎么改比较好呢
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
解决方案 »
- 万能的csdn,谁是编写JAVA高手十万火急救命
- 版上做java的用到1.5新特性的多吗?
- [新手]关于IndexOf的一点问题[HELP]
- 招聘JAVA开发人员
- 关于java应用程序中调用声音的问题,高手请进!!急
- 简单问题求助
- 小弟初学JAVA,请教两个问题,请大家帮忙
- 请问java里有没有一个方法做开方的
- jboss 3.0 + microsoft sqlserver jdbc driver 的奇怪问题,如能解决,200分相赠
- 请问谁有那种能根据所给数值实时变化的曲线图的applet啊?一定给分!
- 我在我们公司的项目中怎么没有看到HIBERNATE映射文件有关系呢,都是单独的
- 菜鸟问题:就是想问下如果我要在一个类中调用另一个类中的tf.getText()中的内容,该怎么弄呢?
/*
* 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();
}
}
}
一个位置上可不能放多个雷呀/*
* 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();
}
}
}
/*
* 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;
}
}
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
还有ArrayList的元素是可以重复的
也就会出现一个位置放多个雷的情况了。。惨,我昨天笔试的时候写了个
Set set = new TreeSet();
回去重写程序才想起来,疯掉了
*
* @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();
}
}
}
我的解决方案! 我想还可以吧!
w = r.nextInt(width);
h = r.nextInt(high);
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]
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似乎随机性还不错
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();
} }}
不是很另类呀想了下,好像还没有考虑雷的数量应该不大于二维数组的元素的个数
应该还要加下面的代码total = total >(W * H)?:(W * H) : total;
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()
{
//显示
}
}肯定有许多问题,刚学并发又没有地方运行下,就当是个方法了。
所以我用while(set.size()<10)来保证取出10个雷
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();
}}直接用一个变量记录雷数,达到跳出循环
你用了ArrayList.contains()方法来避免重复 这也可以 但就没HashSet效率高
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();
}}
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);
}
参照各位的思路写了,不要见笑啊。
# 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;
}
}
}
}
}
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();
}
} }