你怎么总是搞这种题目呢?奇怪
周末有空研究下. 倒没有耗费我整个周末,耗费了我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
温馨提示:答案为网友推荐,仅供参考