luanpaixie262 2016-03-19 04:34 采纳率: 0%
浏览 1558
已采纳

拉丁方针问题如何解决用java方法

这个java问题困惑了好久,作为一个不是共建班的学生,只能靠自己努力好好学,求各位大神教教我看题目要求求代码图片说明

  • 写回答

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===========");
        }
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)