java生成一条线上限定相同数字连在一起的二维数组

问题描述:
1>在10*10的数组上 用户可以定义横、竖、斜相同的数字 最多 为几个,在4-9间选择
2>数组的数字在0-5 随机生成
看了一下 还是 flyingFish211写的比较好
cj18711010500的虽然给出了验证流程但是没有实现用户的定义
辗转天涯 的不符合我的任何要求
才耗费了1个半小时。。
我在学五子棋突然想到的!自己一时也想不来
就是想学习数组的一些比较高级的用法!百度里面找不到!
给自己补充一点知识以后工作才会轻松一点啊!也让后面的可以学习学习

你怎么总是搞这种题目呢?奇怪

周末有空研究下. 倒没有耗费我整个周末,耗费了我1个半小时。。。你要回答下你为啥搞这些???

注意:键盘输入的4个数字是有序的。第一个数字是行限制,第二个是列限制;第三个是正对角线限制(西北方向),第四个是反对角线限制(东北方向)

import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

public class Du {

public static void main(String[] args) {

final int length = 10;

//键盘输入的4个数字依次存放行 列 正对角线 斜对角线的最大值
int[] limitArray = getLimitFromInput();

int rowMax = limitArray[0];
int colMax = limitArray[1];
int diagonalMax = limitArray[2];
int oppDiagonalMax = limitArray[3];

// int rowMax = 5;
// int colMax = 4;
// int diagonalMax = 5;
// int oppDiagonalMax = 5;

int[][] ary = new int[length][length];

fillArrayWithSix(ary);//提前用6来填充因为目标数组不可能出现6

Random rand = new Random();

for(int rowIndex = 0; rowIndex < length; rowIndex++){

for(int colIndex = 0; colIndex < ary[rowIndex].length; colIndex++){
int value = rand.nextInt(6);

boolean isAvaibleForRow = checkRow(ary[rowIndex], rowMax, value);//检查行是否超过最大限制
boolean isAvaibleForCol = checkCol(ary, colIndex, colMax, value);//检查列是否超过最大限制
boolean isAvaibleForDiagonal = checkDiagonal(ary, rowIndex, colIndex, diagonalMax, value);//检查正对角线是否超过最大限制
boolean isAvaibleForOppDiagonal = checkOppDiagonal(ary, rowIndex, colIndex, oppDiagonalMax, value);//检查斜对角线是否超过最大限制

//如果都没有超出最大限制,则赋值,否则继续寻求
if(isAvaibleForRow && isAvaibleForCol && isAvaibleForDiagonal && isAvaibleForOppDiagonal){
ary[rowIndex][colIndex] = value;
}else{
colIndex--;
}
}

}

printArray(ary);

}

private static int[] getLimitFromInput() {

int[] limit = new int[4];
int count = 0;

while(count < limit.length){

if(count == 0){
System.out.println("Please input the limit for row: ");
}else if(count == 1){
System.out.println("Please input the limit for column: ");
}else if(count == 2){
System.out.println("Please input the limit for diagonalMax: ");
}else if(count == 3){
System.out.println("Please input the limit for oppsite diagonalMax: ");
}

try{
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();

if(input < 4 || input > 9){
System.out.println("Invalid digit value, please input a digit(4-9)");
continue;
}

limit[count] = input;
count++;
} catch(InputMismatchException mismatchExp){
System.out.println("Invalid digit format. Please input a digit between 4 and 9");
}
}

return limit;
}

private static boolean checkOppDiagonal(int[][] ary, int rowIndex, int colIndex, int oppDiagonalMax, int value) {
int count = 0;

if(rowIndex < colIndex){

int base = colIndex - rowIndex;
int maxCol = ary[rowIndex].length;
int row = 0;

while(base < maxCol){
if(ary[row++][base++] == value){
count++;
}
}

}else if(rowIndex > colIndex){
int base = rowIndex - colIndex;
int col = 0;

while(base < ary.length){
if(ary[base++][col++] == value){
count++;
}
}

}

return count < oppDiagonalMax;
}

private static boolean checkDiagonal(int[][] ary, int rowIndex, int colIndex, int max, int value) {

final int length = ary.length;

final int indexSum = rowIndex + colIndex;

int count = 0;

if(indexSum < length){
for(int row = indexSum, col = 0; row >= 0; row--){
if(ary[row][col++] == value){
count++;
}
}
}else if(indexSum >= length){

int col = indexSum - length;
int rowBase = ary.length -1;

while(col < ary[rowIndex].length){

if(ary[rowBase--][col] == value){
count++;
}

col++;
}

}

return count < max;
}

private static boolean checkCol(int[][] ary, int colIndex, int colMax, int value) {

int count = 0;

for(int i = 0, len = ary.length; i < len; i++){
if(ary[i][colIndex] == value){
count++;
}
}

return count < colMax;
}

private static boolean checkRow(int[] ary, int rowMax, int value) {

int count = 0;

for(int i = 0, len = ary.length; i < len; i++){
if(ary[i] == value){
count++;
}
}

return count < rowMax;
}

//Impossible to have 6 in the array, fill it with 6
private static void fillArrayWithSix(int[][] ary) {

for(int i = 0, len = ary.length; i < len; i++){
for(int j = 0, length = ary[i].length; j < length; j++){
ary[i][j] = 6;
}
}

}

private static void printArray(int[][] ary) {

for(int i = 0, len = ary.length; i < len; i++){
for(int j = 0, length = ary[i].length; j < length; j++){
System.out.print(ary[i][j] + " ");
}

System.out.println();
}

}
}

--------------------运行结果1
Please input the limit for row:
a
Invalid digit format. Please input a digit between 4 and 9
Please input the limit for row:
3
Invalid digit value, please input a digit(4-9)
Please input the limit for row:
5
Please input the limit for column:
4
Please input the limit for diagonalMax:
12.3
Invalid digit format. Please input a digit between 4 and 9
Please input the limit for diagonalMax:
5
Please input the limit for oppsite diagonalMax:
6
3 0 1 5 2 2 2 2 2 3
0 2 5 3 1 0 2 0 0 0
4 1 1 2 1 0 2 5 1 0
5 1 0 5 5 0 1 1 4 2
3 0 2 5 1 4 3 1 0 5
4 2 2 3 3 3 2 5 0 1
2 3 2 3 1 2 0 0 3 1
3 0 5 1 2 3 3 1 1 3
0 2 0 2 2 4 4 1 2 3
4 3 0 4 2 5 5 0 1 4

------------------testing 2
Please input the limit for row:
5
Please input the limit for column:
7
Please input the limit for diagonalMax:
6
Please input the limit for oppsite diagonalMax:
8
1 1 0 5 0 4 5 4 2 1
3 5 5 1 3 4 0 0 5 2
4 4 4 5 2 3 4 3 2 1
3 4 3 0 5 4 2 4 5 5
3 0 2 5 4 0 2 3 3 5
0 5 1 2 2 4 1 0 2 1
3 2 3 3 5 5 0 1 5 3
5 5 5 2 3 4 2 0 5 3
5 1 2 0 5 2 5 1 1 4
5 3 5 5 0 3 4 1 3 4

----------------testing 3
Please input the limit for row:
9
Please input the limit for column:
9
Please input the limit for diagonalMax:
8
Please input the limit for oppsite diagonalMax:
9
1 3 1 5 0 5 5 1 3 4
1 1 1 0 4 0 3 2 5 2
2 5 1 3 3 2 5 0 4 2
5 4 2 2 1 2 4 3 0 4
4 2 1 2 5 4 4 5 4 0
1 1 0 4 0 1 1 1 5 1
2 4 2 0 5 3 3 0 3 1
3 0 1 5 3 5 2 4 0 2
4 2 0 5 4 4 4 5 1 0
0 1 0 0 4 4 2 3 4 0

-----------------用最小的全部是4
Please input the limit for row:
4
Please input the limit for column:
4
Please input the limit for diagonalMax:
4
Please input the limit for oppsite diagonalMax:
4
5 4 4 4 2 4 2 1 1 5
1 4 4 5 3 5 3 0 0 4
1 4 1 2 0 0 0 5 4 4
2 4 5 5 2 2 2 4 3 1
4 2 3 3 2 2 3 4 2 0
1 3 0 5 4 4 1 5 5 0
0 5 5 0 0 0 4 4 5 4
2 1 4 3 3 3 2 3 0 2
2 3 2 3 4 3 1 5 1 3
4 1 1 2 2 0 4 3 2 1
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-01-12
如果值需要一个符合这个特征的数组的话,
import java.io.InputStreamReader;
import java.io.BufferedReader;

public class NewArray {

public static void main(String[] args) {
while(true){
int[][] arry= new int[10][10];
int limitValue=-1;
try{
System.out.print("请输入限定值(0值退出):");
InputStreamReader re=new InputStreamReader(System.in);
BufferedReader reader=new BufferedReader(re);
String line=reader.readLine();
limitValue=Integer.parseInt(line);
}catch(Exception e){}
if(limitValue==0) break;
if(limitValue==-1) {
System.out.println("无效,请重新输入!");
continue;
}
for(int i=0;i<arry.length;i++){
int value=i%5;
for(int j=0;j<arry[i].length;j++){
arry[i][j]=value;
if((j+1)%limitValue==0) value++;
}
}

System.out.println("************************************************************");
for(int i=0;i<arry.length;i++)
{
for(int j=0;j<arry[i].length;j++)
{
System.out.print(arry[i][j]+" ");
}
System.out.println();
}
}
}
}
第2个回答  2011-01-10
package com.cj18711010500.test;

import java.util.Random;

public class NewArray {
private int x;
private int y;
private int z;
private int random;

public NewArray(int x, int y, int z,int random) {
this.x = x;
this.y = y;
this.z = z;
this.random=random;
}

public int[][] produce() {

int clone[][] = new int[10][10];
for (int i = 0; i < 10; i++) {
int[] two = new int[10];
for (int j = 0; j < 10; j++) {
two[j] = this.tryint(this.getArry(clone, i, j));
clone[i] = two;
}
clone[i] = two;
}
return clone;
}

public int[][] getArry(int[][] source, int y, int x) {

int[][] aim = new int[y + 1][];
for (int i = 0; i < y; i++) {
int newArry[] = new int[source[0].length];
for (int j = 0; j < source[0].length; j++) {
newArry[j] = source[i][j];
}
aim[i] = newArry;
}
int newArry[] = new int[x + 1];
for (int i = 0; i < x; i++) {
newArry[i] = source[y][i];
}
aim[y] = newArry;
return aim;
}

public int tryint(int[][] arry) {
int num = 0;
int yl = arry.length;
int xl = arry[yl - 1].length;
int X = xl - 1;
int Y = yl - 1;

while (true) {
Random r = new Random();
int value = r.nextInt(this.random);
int[] x = arry[Y];
int[] y = this.getYs(arry);
int[] zone = this.getZ_One(arry);
int[] ztwo = this.getZ_Two(arry);

for (int i = 0; i < arry.length; i++) {

for (int j = 0; j < arry[i].length; j++) {

System.out.print(arry[i][j] + " ");
}
System.out.println();
}

System.out
.println("----------------------------------------------");
System.out.println("待验证值" + value);
System.out.print("xIsFull " + this.xIsFull(x, value));
this.println(" ", x);
System.out.print("yIsFull " + this.yIsFull(y, value));
this.println(" ", y);
System.out.print("zIsFullone/ " + this.zIsFull(zone, value));
this.println(" ", zone);
System.out.print("zIsFulltwo\\ " + this.zIsFull(ztwo, value));
this.println(" ", ztwo);
System.out
.println("----------------------------------------------");
if (!this.xIsFull(x, value)) {
if (!this.yIsFull(y, value)) {
if (!this.zIsFull(ztwo, value)) {
if (!this.zIsFull(zone, value)) {
num = value;
break;
}
}
}
}
}
return num;
}

public boolean xIsFull(int[] arry, int num) {
boolean flage = false;
int count = 0;
for (int i = 0; i < arry.length - 1; i++) {
if (arry[i] == num) {
count += 1;
}
}
if (this.x <= count) {
flage = true;
}
return flage;
}

public boolean yIsFull(int[] arry, int num) {
boolean flage = false;
int count = 0;
for (int i = 0; i < arry.length; i++) {
if (arry[i] == num) {
count += 1;
}
}
if (this.y <= count) {
flage = true;
}
return flage;
}

public boolean zIsFull(int[] arry, int num) {
boolean flage = false;
int count = 0;
for (int i = 0; i < arry.length; i++) {
if (arry[i] == num) {
count += 1;
}
}
if (this.z <= count) {
flage = true;
}
return flage;
}

public int[] getZ_Two(int[][] arry) {
int yl2 = arry.length;
int xl2 = arry[yl2 - 1].length;
int X = xl2 - 1;
int Y = yl2 - 1;

int xl = arry[0].length;
int yl = arry.length;
int sum = 0;
if ((xl2 + yl2) < xl || X == 0) {
if (xl2 <= yl2) {
sum = X;
} else {
sum = yl2;
}
} else if ((xl2 + yl2) >= xl) {
sum = yl2;
}

int[] newArry = new int[sum];
for (int i = 0; i <newArry.length; i++) {
newArry[i] = arry[Y <= 0 ? 0 : Y--][X <= 0 ? 0 : X--];
}
return newArry;
}
public int[] getZ_One(int[][] arry) {
int yl2 = arry.length;
int xl2 = arry[yl2 - 1].length;
int X = xl2 - 1;
int Y = yl2 - 1;

int xl = arry[0].length;
int yl = arry.length;
int sum = 0;
if ((xl2 + yl2) < xl) {
if (xl2 <= yl2) {
sum = yl2;
} else {
sum = yl2;
}
} else if ((xl2 + yl2) >= xl) {
sum = xl-X;
}
int[] newArry = new int[sum];
for (int i = 0; i <newArry.length; i++) {
newArry[i] = arry[Y <= 0 ? 0 : Y--][X >=(xl-1) ?(xl-1):X++];
}
return newArry;
}

public int[] getYs(int[][] arry) {
int yl = arry.length;
int xl = arry[yl - 1].length;
int X = xl - 1;
int Y = yl - 1;
int[] newArry = new int[Y];
for (int i = 0; i < newArry.length; i++) {
newArry[i] = arry[i][X];
}
return newArry;
}

public void println(String title, int[] arry) {
StringBuffer str = new StringBuffer(title);
for (int i = 0; i < arry.length; i++) {
str.append(arry[i]);
}
System.out.println(str.toString());
}
public static void main(String[] args) {
int[][] arry= new NewArray(4,4,4,5).produce();//数字不能太小,太小会出现死循环
System.out.println("\t\t最终结果 别忘了 加分啊");
System.out.println("************************************************************");
for(int i=0;i<arry.length;i++)
{
for(int j=0;j<arry[i].length;j++)
{
System.out.print(arry[i][j]+" ");
}
System.out.println();
}
//详细流程请看控制台信息
//还有在多给点分吧?写这个真的很吃亏,我的星期天也没了
}

}
相似回答