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

问题描述

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

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

解决方案二:

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

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

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的英雄某问出处,平时自己多动手就会有提高的。

解决方案三:

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

时间: 2024-09-10 06:57:25

java源码-拉丁方针问题如何解决用java方法的相关文章

解密随机数生成器(二)——从java源码看线性同余算法

Random Java中的Random类生成的是伪随机数,使用的是48-bit的种子,然后调用一个linear congruential formula线性同余方程(Donald Knuth的编程艺术的3.2.1节) 如果两个Random实例使用相同的种子,并且调用同样的函数,那么生成的sequence是相同的 也可以调用Math.random()生成随机数 Random实例是线程安全的,但是并发使用Random实例会影响效率,可以考虑使用java.util.concurrent.ThreadL

java源码-求大神告诉源码,真的不太会

问题描述 求大神告诉源码,真的不太会 面向对象来写,可是不太会,求大神源码???????????????????????????????????????????????????????????????????????? 解决方案 package com.njupt.www; import java.util.Date; public class Employee { private int num; private String name; private String position; pr

java源码阅读方法以及经验

问题描述 java源码阅读方法以及经验 如何更好的阅读java源码,更注重阅读哪些包里面的源码,当然连好的阅读源码的工具也说明一下更好了 解决方案 我在这里假设你在问怎么阅读jdk的源码,java源码这个名字有点奇怪. 你可以build 一个fast debug版本,然后使用debugger去调试你的程序,这样对程序是怎么调用的有很直观的视图. 其次,可以看看jdk里面的regression tests,里面有很多例子. 其次,openjdk提供了netbean的jdk project,你可以很

关于生成java源码的问题

问题描述 最近要做一个通过java源码生成另一套java源码,如根据以下代码:publicclasshelloword{publicStringtest="";publicvoidtest(){/*语句...*/}publicStringtest(){/*语句...*/returnresult;}} 生成如下代码:publicclasshelloword{publicStringtest="";publicvoidtest(){}publicStringtest()

对象-java源码中一个抽象类初始化方法中包含一个super(),该怎么理解

问题描述 java源码中一个抽象类初始化方法中包含一个super(),该怎么理解 package org.apache.http.params; import java.util.Set; /** * Abstract base class for parameter collections. * Type specific setters and getters are mapped to the abstract, * generic getters and setters. * * @si

技术-用jsoup抓取网页获取网页源码的时候,得到的源码和在网站上面点击右键查看源码不相同,怎么解决

问题描述 用jsoup抓取网页获取网页源码的时候,得到的源码和在网站上面点击右键查看源码不相同,怎么解决 用jsoup抓取网页获取网页源码的时候,得到的源码和在网站上面点击右键查看源码不相同,怎么解决 Document doc = Jsoup.parse(new URL(url), 5000); 解决方案 一部分html是ajax异步加载的,你得用fiddler调试,得到这些请求,照着写 解决方案二: C++ 抓取网页的源码获取网页的源码获取网页源码工具类

java源码-输入年月日信息问题?完全不知道啊

问题描述 输入年月日信息问题?完全不知道啊 这个题目怎么实现啊,一点点的思路都没有,不知道如何去计算,谁能给我个java源码啊???????????????????????? 解决方案 Calendar a = Calendar.getInstance(); a.set(2016, 0, 1); System.out.println(a.get(a.DAY_OF_YEAR)); //当前年月日在当前年的第几天 a.set(年,月-1,日). 解决方案二: #include <stdio.h>

myeclipse-怎样在自己电脑上运行java源码

问题描述 怎样在自己电脑上运行java源码 本人小白一枚,以前用myeclipse做过网站,今天在这里下载了网络白板的java源码,但是用myeclipse导入之后为什么运行不了啊,请问应该怎样能让java'源码在自己电脑上跑起来啊 解决方案 例如d盘下,CaiShu.java 打开开始-运行-输入cmd回车打开命令行窗口 cd d: javac CaiShu.java 将java文件编译为字节码文件 然后用 java Caishu 运行程序. 以上操作是在环境变量配置正确的前提下进行

关于查看Java源码问题

问题描述 关于查看Java源码问题 为什么在Java源码中看不到迭代器Iterator里相关方法是怎么实现的,比如next(),hasnext()等 解决方案 迭代器是接口,方法是由它的子类实现的.可以使用快捷键,双击选中方法名,然后按下Ctrl加T就能看到实现类了. 解决方案二: Java源码问题查看java源码 Java source code怎么查看java api的源码? 解决方案三: 这个你可以在网上直接百度,参考源码啊