2 luanpaixie262 luanpaixie262 于 2016.03.19 12:34 提问

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

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

3个回答

zqftisson
zqftisson   2016.03.19 18: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===========");
    }
}


zqftisson
zqftisson   2016.03.19 17:52

其实挺简单的。所谓的拉丁方阵,你研究它的特点,无非就是行列相加的结果相同。标准拉丁方阵只是多了一点限制。

根据这些特点,很容易就可以编码了。

package questions;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class LatinArrayHandler {

    private static int STOP_SIGNAL = 0;

    private boolean isLatinArray(int stepNum, List<Integer> sourceArray) {
        int firstRowCount = 0;
        for (int i = 0; i < stepNum; i++) {
            firstRowCount += sourceArray.get(i);
        }
//      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 (countRowNums(stepNum, rowPointer, sourceArray) != firstRowCount
                    || countColNums(stepNum, colPointer, sourceArray) != firstRowCount) {
                return false;
            } else {
                rowPointer += stepNum;
                colPointer++;
            }
        }
        return true;
    }

    private int countRowNums(int stepNum, int curPointer,
            List<Integer> sourceArray) {
        int rowTotal = 0;
        for (int i = 0; i < stepNum; i++) {
            rowTotal += sourceArray.get(curPointer);
            curPointer++;
        }
//      System.out.println(" this row total = " + rowTotal);
        return rowTotal;
    }

    private int countColNums(int stepNum, int curPointer,
            List<Integer> sourceArray) {
        int colTotal = 0;
        for (int i = 0; i < stepNum; i++) {
            colTotal += sourceArray.get(curPointer);
            curPointer += stepNum;
        }
//      System.out.println(" this col total = " + colTotal);
        return colTotal;
    }

    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===========");
    }
}


结果大概是这样

图片说明

我没帮你做异常处理和输入检测。

ps:做IT的英雄某问出处,平时自己多动手就会有提高的。

zqftisson
zqftisson   2016.03.19 18:26

吃饭的时候想到我漏了一个重要的特性:每一行(或列)中的元素应该是互斥的。在你的题目要求中就是说每一行(列)都应该包含有不一样的正整数。
你把这个加强条件也加进去吧,改造一下 isLatinArray() 和 isStandardLatinArray()。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!