這個java問題困惑了好久,作為一個不是共建班的學生,只能靠自己努力好好學,求各位大神教教我看題目要求求代碼
改造後
package questions;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class LatinArrayHandler {
private static int STOP_SIGNAL = 0;
private boolean isLatinArray(int stepNum, List<Integer> sourceArray) {
Map<Integer, Integer> itemMap = new HashMap<Integer, Integer>();
int firstRowCount = 0;
for (int i = 0; i < stepNum; i++) {
firstRowCount += sourceArray.get(i);
itemMap.put(sourceArray.get(i), sourceArray.get(i));
}
if (itemMap.size() != stepNum) {
return false;
}
// System.out.println("firstRowCount=" + firstRowCount);
int rowPointer = 0;
int colPointer = 0;
for (int j = 0; j < stepNum; j++) {
// System.out.println("========the [" + j + "] row and col count======");
if (!checkRowNums(stepNum, rowPointer, sourceArray, firstRowCount, itemMap)
|| !countColNums(stepNum, colPointer, sourceArray, firstRowCount, itemMap)) {
return false;
} else {
rowPointer += stepNum;
colPointer++;
}
}
return true;
}
private boolean checkRowNums(int stepNum, int curPointer, List<Integer> sourceArray, int firstRowCount, Map<Integer, Integer> itemMap) {
int rowTotal = 0;
for (int i = 0; i < stepNum; i++) {
if (!itemMap.containsKey(sourceArray.get(curPointer))) {
return false;
} else {
rowTotal += sourceArray.get(curPointer);
curPointer++;
}
}
// System.out.println(" this row total = " + rowTotal);
if (rowTotal != firstRowCount) {
return false;
}
return true;
}
private boolean countColNums(int stepNum, int curPointer,
List<Integer> sourceArray, int firstRowCount, Map<Integer, Integer> itemMap) {
int colTotal = 0;
for (int i = 0; i < stepNum; i++) {
if (!itemMap.containsKey(sourceArray.get(curPointer))) {
return false;
} else {
colTotal += sourceArray.get(curPointer);
curPointer += stepNum;
}
}
// System.out.println(" this col total = " + colTotal);
if (colTotal != firstRowCount) {
return false;
}
return true;
}
private boolean isStandardLatinArray(int stepNum, List<Integer> sourceArray) {
int rowPointer = 0;
int colPointer = 0;
return isSequenceRow(stepNum, rowPointer, sourceArray)
&& isSequenceCol(stepNum, colPointer, sourceArray);
}
private boolean isSequenceRow(int stepNum, int curPointer,
List<Integer> sourceArray) {
int expectedNum = sourceArray.get(curPointer) + 1;
for (int i = 1; i < stepNum; i++) {
if (expectedNum != sourceArray.get(i)) {
return false;
} else {
expectedNum++;
}
}
return true;
}
private boolean isSequenceCol(int stepNum, int curPointer,
List<Integer> sourceArray) {
int expectedNum = sourceArray.get(curPointer) + 1;
for (int i = 1; i < stepNum; i++) {
if (expectedNum != sourceArray.get(i)) {
return false;
} else {
expectedNum += stepNum;
}
}
return true;
}
private void printResult(boolean isLatinArray, boolean isStandardLatinArray) {
if (isLatinArray) {
if (isStandardLatinArray) {
System.out.println(">>>>>checking result == the array is standard latin array == 2");
} else {
System.out.println(">>>>>checking result == the array is latin array == 1");
}
} else {
System.out.println(">>>>>checking result == the array not latin array == 0");
}
}
public static void main(String[] args) throws IOException {
LatinArrayHandler latinArrayHandler = new LatinArrayHandler();
int stepNum = 0;
while (true) {
System.out.println("===== start the latin array checking===========");
System.out.println("please input the step number>>");
Scanner scanner = new Scanner(new InputStreamReader(System.in));
stepNum = Integer.parseInt(scanner.nextLine());
if (stepNum <= STOP_SIGNAL) {
scanner.close();
break;
} else {
List<Integer> sourceList = new ArrayList<Integer>();
System.out.println("the step number is [" + stepNum + "], please input the array with the format:" + stepNum + " * " + stepNum);
for (int i = 0; i < stepNum; i++) {
String line = scanner.nextLine();
String[] numsLine = line.split(" ");
for (String str : numsLine) {
sourceList.add(Integer.parseInt(str));
}
}
boolean isLatinArray = latinArrayHandler.isLatinArray(stepNum, sourceList);
boolean isStandardLatinArray = latinArrayHandler.isStandardLatinArray(stepNum, sourceList);
latinArrayHandler.printResult(isLatinArray, isStandardLatinArray);
}
}
System.out.println("===== stopped the latin array checking===========");
}
}