3条回答
- Sam_qifa 2016-03-19 10:42关注
改造后
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==========="); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报无用 1
悬赏问题
- ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
- ¥15 如何在scanpy上做差异基因和通路富集?
- ¥20 关于#硬件工程#的问题,请各位专家解答!
- ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
- ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
- ¥30 截图中的mathematics程序转换成matlab
- ¥15 动力学代码报错,维度不匹配
- ¥15 Power query添加列问题
- ¥50 Kubernetes&Fission&Eleasticsearch
- ¥15 報錯:Person is not mapped,如何解決?