题目:
6|8|2 : 一个号码正确且位置正确;
6|1|4 : 一个号码正确但是位置不正确;
2|0|6 : 二个号码正确但是位置不正确;
7|3|8 : 没有一个号码正确;
8|7|0 : 一个号码正确但是位置不正确;
Java程序:
package com.company;
import java.util.*;
public class Main {
/**
* 输入的问题容器
*/
private static class InputContainer {
public InputContainer() {
}
/**
* ItemList ~ 数据容器
*/
private ArrayList<InputItem> itemList;
/**
* ItemList ~ 数据容器
*
* @return
*/
public ArrayList<InputItem> getItemList() {
return itemList;
}
/**
* ItemList ~ 数据容器
*
* @param theValue
*/
public void setItemList(ArrayList<InputItem> theValue) {
itemList = theValue;
}
public String printInfoGet() {
StringBuilder sb = new StringBuilder();
for (InputItem item : getItemList()) {
sb.append(item.printInfoGet());
sb.append('\r');
sb.append('\n');
}
return sb.toString();
}
/**
* 获得答案的最大列数
*
* @return
*/
public int maxInputDataLengthGet() {
int theResult = 0;
int iTmp = 0;
for (InputItem item : getItemList()) {
iTmp = item.getInputData().length;
if (iTmp > theResult)
theResult = iTmp;
}
return theResult;
}
/**
* 获得相关数字
* @return
*/
public ArrayList<Integer> inputNumArrayGet() {
ArrayList<Integer> theResult = new ArrayList<Integer>();
for (InputItem item : getItemList()) {
for (int i : item.getInputData()) {
if (!theResult.contains(i)) {
theResult.add(i);
}
}
}
return theResult;
}
public String commandPrint() {
StringBuilder theResult = new StringBuilder();
boolean firstFlag = true;
for (InputItem item : getItemList()) {
if (firstFlag) {
firstFlag = false;
} else {
theResult.append("#");
}
theResult.append(item.commandPrint());
}
return theResult.toString();
}
}
/**
* 0~10的数字转中文显示
*
* @param num
* @return
*/
private static String numToCnShow(int num) {
switch (num) {
case 0:
return "零";
case 1:
return "一";
case 2:
return "二";
case 3:
return "三";
case 4:
return "四";
case 5:
return "五";
case 6:
return "六";
case 7:
return "七";
case 8:
return "八";
case 9:
return "九";
case 10:
return "十";
default:
return Integer.toString(num);
}
}
/**
* 输入条件的单项
*/
private static class InputItem {
public InputItem() {
}
/**
* InputData ~ 输入值
*/
private int[] inputData;
/**
* InputData ~ 输入值
*
* @return
*/
public int[] getInputData() {
return inputData;
}
/**
* InputData ~ 输入值
*
* @param theValue
*/
public void setInputData(int[] theValue) {
inputData = theValue;
}
/**
* AllRightCount ~ 号码和位置正确的个数
*/
private int allRightCount;
/**
* AllRightCount ~ 号码和位置正确的个数
*
* @return
*/
public int getAllRightCount() {
return allRightCount;
}
/**
* AllRightCount ~ 号码和位置正确的个数
*
* @param theValue
*/
public void setAllRightCount(int theValue) {
allRightCount = theValue;
}
/**
* NumRightCount ~ 号码正确的个数
*/
private int numRightCount;
/**
* NumRightCount ~ 号码正确的个数
*
* @return
*/
public int getNumRightCount() {
return numRightCount;
}
/**
* NumRightCount ~ 号码正确的个数
*
* @param theValue
*/
public void setNumRightCount(int theValue) {
numRightCount = theValue;
}
public String printInfoGet() {
StringBuilder sb = new StringBuilder();
boolean firstFlag = true;
for (int i : getInputData()) {
if (firstFlag) {
firstFlag = false;
} else
sb.append("|");
sb.append(i);
}
sb.append(" : ");
if (getAllRightCount() == 0 && getNumRightCount() == 0) {
sb.append("没有一个号码正确;");
} else {
int count = getAllRightCount();
if (count > 0) {
sb.append(numToCnShow(count) + "个号码正确且位置正确;");
}
count = getNumRightCount();
if (count > 0) {
sb.append(numToCnShow(count) + "个号码正确但是位置不正确;");
}
}
return sb.toString();
}
private boolean theNumIsAllRight(int theNum, int fillIndex) {
int iLen = getInputData().length;
if (fillIndex >= iLen) {
return false;
}
if (getInputData()[fillIndex] == theNum) {
return true;
}
return false;
}
private boolean theNumIsInRight(int theNum, int fillIndex) {
int iLen = getInputData().length;
for (int i = 0; i < iLen; ++i) {
if (getInputData()[i] == theNum) {
if (i == fillIndex) {
return false;
} else {
return true;
}
}
}
return false;
}
private int allRightGetTotal(int fillIndex, int[] resultArray) {
int theResult = 0;
int loopLen = fillIndex + 1;
for (int i = 0; i < loopLen; ++i) {
if (theNumIsAllRight(resultArray[i], i)) {
++theResult;
}
}
return theResult;
}
private int numRightGetTotal(int fillIndex, int[] resultArray) {
int theResult = 0;
int loopLen = fillIndex + 1;
for (int i = 0; i < loopLen; ++i) {
if (theNumIsInRight(resultArray[i], i)) {
++theResult;
}
}
return theResult;
}
public boolean dataIsOk(int theNum, int fillIndex, int[] resultArray) {
boolean endFlag = false;
if(fillIndex==resultArray.length-1){
endFlag = true;
}
int iCount = allRightGetTotal(fillIndex, resultArray);
if (iCount > getAllRightCount()) {
return false;
}
else {
if (endFlag) {
if (iCount != getAllRightCount()) {
return false;
}
}
}
iCount = numRightGetTotal(fillIndex, resultArray);
if (iCount > getNumRightCount()) {
return false;
}
else {
if (endFlag) {
if (iCount != getNumRightCount()) {
return false;
}
}
}
return true;
}
public String commandPrint() {
StringBuilder theResult = new StringBuilder();
boolean firstFlag = true;
for(int i:getInputData()){
if(firstFlag){
firstFlag=false;
}else{
theResult.append(",");
}
theResult.append(i);
}
theResult.append(";");
theResult.append(getAllRightCount());
theResult.append(",");
theResult.append(getNumRightCount());
return theResult.toString();
}
}
private static int[] inputDataCreate(String str) {
if (str == null || str.length() == 0) {
return null;
}
str = str.trim();
if (str.length() == 0) {
return null;
}
String[] sA = str.split(",");
int iLen = sA.length;
int i = 0;
int[] theResult = new int[iLen];
int item = 0;
for (i = 0; i < iLen; ++i) {
item = intGetByStr(sA[i], -1);
theResult[i] = item;
}
return theResult;
}
private static void inputItemSetCount(InputItem theResult, String str) {
if (str == null || str.length() == 0) {
return;
}
str = str.trim();
if (str.length() == 0) {
return;
}
String[] sA = str.split(",");
int iLen = sA.length;
for (int i = 0; i < iLen; ++i) {
switch (i) {
case 0:
theResult.setAllRightCount(intGetByStr(sA[i], 0));
break;
case 1:
theResult.setNumRightCount(intGetByStr(sA[i], 0));
break;
}
}
}
/**
* @param str
* @param defaultValue
* @return
*/
private static int intGetByStr(String str, int defaultValue) {
try {
return Integer.parseInt(str);
} catch (NumberFormatException e) {
return defaultValue;
}
}
private static InputItem inputItemCreate(String str) {
if (str == null || str.length() == 0) {
return null;
}
str = str.trim();
if (str.length() == 0) {
return null;
}
String[] sA = str.split(";");
int iLen = sA.length;
if (iLen == 0)
return null;
InputItem theResult = new InputItem();
theResult.setInputData(inputDataCreate(sA[0]));
theResult.setAllRightCount(0);
theResult.setNumRightCount(0);
if (iLen > 1)
inputItemSetCount(theResult, sA[1]);
return theResult;
}
private static InputContainer inputContainerCreate(String str) {
if (str == null || str.length() == 0)
return null;
InputContainer theResult = new InputContainer();
ArrayList<InputItem> itemList = new ArrayList<InputItem>();
theResult.setItemList(itemList);
String[] sA = str.split("#");
int iLen = sA.length;
int i = 0;
InputItem inputItemV = null;
String subStr = null;
for (i = 0; i < iLen; ++i) {
subStr = sA[i];
if (subStr == null || subStr.length() == 0) {
continue;
}
inputItemV = inputItemCreate(subStr);
if (inputItemV == null) {
continue;
}
itemList.add(inputItemV);
}
return theResult;
}
private static void inputContainerPrint(InputContainer inputContainerV) {
if (inputContainerV == null) {
System.out.println("InputContainer is null.");
return;
}
inputContainerV.getItemList();
}
private static boolean theNumIsInArray(int[] resultArray, int theNum) {
for (int i : resultArray) {
if (i == theNum) {
return true;
}
}
return false;
}
/**
* 是否已经填充值
*
* @param resultArray
* @param fillIndex
* @param theNum
* @return
*/
private static boolean theNumIsFillInArray(int[] resultArray, int fillIndex, int theNum) {
for (int i = 0; i < fillIndex; ++i) {
if (resultArray[i] == theNum) {
return true;
}
}
return false;
}
private static boolean exhaustiveToAnswerData(InputContainer inputContainerV, ArrayList<Integer> inputArray, int[] resultArray, int fillIndex, int noArrayValue) {
if (fillIndex == resultArray.length) {
return true;
}
boolean bFlag = false;
for (int theNum : inputArray) {
if (theNumIsFillInArray(resultArray, fillIndex, theNum)) {
continue;
}
resultArray[fillIndex] = theNum;
if(theNum==3&&fillIndex==2)
{
fillIndex = 2;
}
for (InputItem inputItemV : inputContainerV.getItemList()) {
bFlag = inputItemV.dataIsOk(theNum, fillIndex, resultArray);
if (!bFlag)
break;
}
if (!bFlag) {
resultArray[fillIndex] = noArrayValue;
continue;
}
bFlag = exhaustiveToAnswerData(inputContainerV, inputArray, resultArray, fillIndex + 1, noArrayValue);
if (bFlag) {
return true;
}
resultArray[fillIndex] = noArrayValue;
}
return false;
}
private static boolean exhaustiveToAnswer(InputContainer inputContainerV, ArrayList<Integer> inputArray, int[] resultArray, int noArrayValue) {
return exhaustiveToAnswerData(inputContainerV, inputArray, resultArray, 0, noArrayValue);
}
/**
* 获得最大值
*
* @param theList
* @return
*/
private static int maxGetFromArray(ArrayList<Integer> theList) {
int theResult = Integer.MIN_VALUE;
for (int i : theList) {
if (theResult < i) {
theResult = i;
}
}
return theResult;
}
/**
* 输入样例:6,8,2;1,0#6,1,4;0,1#2,0,6;0,2#7,3,8;0,0#8,7,0;0,1
*
* @param args
*/
public static void main(String[] args) {
String input = null;
if (args == null || args.length == 0) {
// 042 //
// input = "6,8,2;1,0#6,1,4;0,1#2,0,6;0,2#7,3,8;0,0#8,7,0;0,1";
// 871 //
input = "6,8,2;0,1#6,1,4;0,1#2,0,6;0,0#7,3,8;0,2#8,7,0;2,0";
}
else {
input = args[0];
}
InputContainer inputContainerV = inputContainerCreate(input);
if (inputContainerV == null) {
System.out.println("input is null.");
return;
}
// 输出题目 //
String str = null;
str = inputContainerV.commandPrint();
System.out.println("输入参数:");
System.out.println(str);
str = inputContainerV.printInfoGet();
System.out.println("题目:");
System.out.println(str);
// 获得答案的最大数组 //
int resultArrayLength = inputContainerV.maxInputDataLengthGet();
ArrayList<Integer> inputArray = inputContainerV.inputNumArrayGet();
int noArrayValue = maxGetFromArray(inputArray) + 1;
int[] resultArray = new int[resultArrayLength];
for (int i = 0; i < resultArrayLength; ++i) {
resultArray[i] = noArrayValue;
}
boolean runFlag = exhaustiveToAnswer(inputContainerV, inputArray, resultArray, noArrayValue);
if (runFlag) {
StringBuilder sbRet = new StringBuilder();
for (int k : resultArray) {
sbRet.append(k);
}
System.out.println("答案:");
System.out.println(sbRet.toString());
} else {
System.out.println("计算失败");
}
}
}
输出:
题目:
6|8|2 : 一个号码正确且位置正确;
6|1|4 : 一个号码正确但是位置不正确;
2|0|6 : 二个号码正确但是位置不正确;
7|3|8 : 没有一个号码正确;
8|7|0 : 一个号码正确但是位置不正确;
答案:
042
程序简单介绍:
1、Java控制台程序,通过args可以传入参数去构建不同的题目
2、本题题目的args参数
输入样例:6,8,2;1,0#6,1,4;0,1#2,0,6;0,2#7,3,8;0,0#8,7,0;0,1
3、构建一个容器类InputContainer ,统一管理输入条件
4、构建一个条件类InputItem,存储输入条件
5、用printInfoGet方法,打印输出题目内容
6、获得答案的最大数组(考虑条件数组不规范的情况)
7、获得所有的数字项
8、用穷举算法和递归一个一个填充数字找出答案