编程实现:
package cn.itcast.test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Reasoning {
public static void main(String[] args) {
String[] choices = {"A","B","C","D"};
String[] answers = new String[10];
for (String first : choices) {
for(String second : choices){
for(String third : choices){
for(String fourth : choices){
for(String fifth : choices){
for(String sixth : choices){
for(String seventh : choices){
for(String eighth : choices){
for(String ninth : choices){
for(String tenth : choices){
answers[0]=first;
answers[1]=second;
answers[2]=third;
answers[3]=fourth;
answers[4]=fifth;
answers[5]=sixth;
answers[6]=seventh;
answers[7]=eighth;
answers[8]=ninth;
answers[9]=tenth;
if(checkAnswer(answers)){
for (int i = 0; i < answers.length; i++) {
System.out
.print(answers[i]+" ");
}
}
}
}
}
}
}
}
}
}
}
}
}
private static boolean checkAnswer(String[] answers) {
if(!checkfirst(answers)){
return false;
}
if(!checksecond(answers)){
return false;
}
if(!checkthird(answers)){
return false;
}
if(!checkfouth(answers)){
return false;
}
if(!checkfifth(answers)){
return false;
}
if(!checksixth(answers)){
return false;
}
if(!checkseven(answers)){
return false;
}
if(!checkeighth(answers)){
return false;
}
if(!checkninth(answers)){
return false;
}
if(!checktenth(answers)){
return false;
}
return true;
}
private static boolean checktenth(String[] answers) {
// TODO Auto-generated method stub
List<String> list = Arrays.asList(answers);
int countA = Collections.frequency(list, "A");
int countB = Collections.frequency(list, "B");
int countC = Collections.frequency(list, "C");
int countD = Collections.frequency(list, "D");
int max=countA,min=countA;
if(countB>max){
max=countB;
}
if(countC>max){
max=countC;
}
if(countD>max){
max=countD;
}
if(countB<min){
min=countB;
}
if(countC<min){
min=countC;
}
if(countD<min){
min=countD;
}
int result = max-min;
String choice = answers[9];
if(choice.equals("A")){
return result==3;
}
if(choice.equals("B")){
return result==2;
}
if(choice.equals("C")){
return result==4;
}
if(choice.equals("D")){
return result==1;
}
return false;
}
private static boolean checkninth(String[] answers) {
// TODO Auto-generated method stub
String choice = answers[8];
if(choice.equals("A")){
boolean f1 = answers[0].equals(answers[5]);// 1 6
boolean f2 = answers[5].equals(answers[4]);//6 5
boolean f3 = f1!=f2;//f1!=f2
return f3;
}
if(choice.equals("B")){
boolean f1 = answers[0].equals(answers[5]);// 1 6
boolean f2 = answers[9].equals(answers[4]);//10 5
boolean f3 = f1!=f2;//f1!=f2
return f3;
}
if(choice.equals("C")){
boolean f1 = answers[0].equals(answers[5]);// 1 6
boolean f2 = answers[1].equals(answers[4]);//2 5
boolean f3 = f1!=f2;//f1!=f2
return f3;
}
if(choice.equals("D")){
boolean f1 = answers[0].equals(answers[5]);// 1 6
boolean f2 = answers[8].equals(answers[4]);//9 5
boolean f3 = f1!=f2;//f1!=f2
return f3;
}
return false;
}
private static boolean checkeighth(String[] answers) {
// TODO Auto-generated method stub
String choice = answers[7];
char firstchoice = answers[0].charAt(0);
if(choice.equals("A")){
char sevenchoice = answers[6].charAt(0);
return Math.abs(firstchoice-sevenchoice)>1;
}
if(choice.equals("B")){
char fivechoice = answers[4].charAt(0);
return Math.abs(firstchoice-fivechoice)>1;
}
if(choice.equals("C")){
char twochoice = answers[1].charAt(0);
return Math.abs(firstchoice-twochoice)>1;
}
if(choice.equals("D")){
char tenchoice = answers[9].charAt(0);
return Math.abs(firstchoice-tenchoice)>1;
}
return false;
}
private static boolean checkseven(String[] answers) {
// TODO Auto-generated method stub
List<String> list = Arrays.asList(answers);
int countA = Collections.frequency(list, "A");
int countB = Collections.frequency(list, "B");
int countC = Collections.frequency(list, "C");
int countD = Collections.frequency(list, "D");
int countminab = countA<countB?countA:countB;
int countmincd = countC<countD?countC:countD;
int countmin =countminab<countmincd?countminab:countmincd;
if(answers[6].equals("A")){
return countC==countmin;
}
if(answers[6].equals("B")){
return countB==countmin;
}
if(answers[6].equals("C")){
return countA==countmin;
}
if(answers[6].equals("D")){
return countD==countmin;
}
return false;
}
private static boolean checksixth(String[] answers) {
// TODO Auto-generated method stub
if(answers[5].equals("A")){//248//137
return answers[1].equals(answers[3])&&answers[1].equals(answers[7]);
}
if(answers[5].equals("B")){//268//157
return answers[1].equals(answers[5])&&answers[1].equals(answers[7]);
}
if(answers[5].equals("C")){//3108//297
return answers[2].equals(answers[9])&&answers[2].equals(answers[7]);
}
if(answers[5].equals("D")){//598//487
return answers[4].equals(answers[8])&&answers[4].equals(answers[7]);
}
return false;
}
private static boolean checkfifth(String[] answers) {
// TODO Auto-generated method stub
if(answers[4].equals("A")){//58//47
return answers[4].equals(answers[7]);
}
if(answers[4].equals("B")){//54//43
return answers[4].equals(answers[3]);
}
if(answers[4].equals("C")){//59//48
return answers[4].equals(answers[8]);
}
if(answers[4].equals("D")){//57//46
return answers[4].equals(answers[6]);
}
return false;
}
private static boolean checkfouth(String[] answers) {
// TODO Auto-generated method stub
if(answers[3].equals("A")){//15//04
return answers[0].equals(answers[4]);
}
if(answers[3].equals("B")){//27//16
return answers[1].equals(answers[6]);
}
if(answers[3].equals("C")){//19//08
return answers[0].equals(answers[8]);
}
if(answers[3].equals("D")){//610//59
return answers[5].equals(answers[9]);
}
return false;
}
private static boolean checkthird(String[] answers) {
// TODO Auto-generated method stub
String choice = answers[2];
switch (choice) {
case "A"://2==4==6!=3//1352
if(answers[1].equals(answers[3])
&&answers[1].equals(answers[5])&&!answers[5].equals(answers[2])){
return true;
}
break;
case"B"://2==3==4!=6//1235
if(answers[1].equals(answers[2])
&&answers[1].equals(answers[3])&&!answers[3].equals(answers[5])){
return true;
}
break;
case"C"://3==4==6!=2//2351
if(answers[2].equals(answers[3])
&&answers[2].equals(answers[5])&&!answers[5].equals(answers[1])){
return true;
}
break;
case"D"://2==3==6!=4//1253
if(answers[1].equals(answers[2])
&&answers[1].equals(answers[5])&&!answers[5].equals(answers[3])){
return true;
}
break;
default:
break;
}
return false;
}
private static boolean checksecond(String[] answers) {
// TODO Auto-generated method stub
String choice = answers[1];
if(choice.equals("A")){
return answers[4].equals("C");
}
if(choice.equals("B")){
return answers[4].equals("D");
}
if(choice.equals("C")){
return answers[4].equals("A");
}
if(choice.equals("D")){
return answers[4].equals("B");
}
return false;
}
private static boolean checkfirst(String[] answers) {
// TODO Auto-generated method stub
return true;
}
}