Directory
1 Problem Description
2 Solutions
1 problem description
A total of 5 red scarves, numbered A, B, C, D, E, the grandmother was one of them to help cross the road.
Five red scarves speak each other:
A: Neither I nor e have helped granny
B: Granny is a C and E, one of them helped me through the street.
C: Granny's been helped by me and D.
D:b and C are not helping granny across the street.
E: I didn't help grandma.
It is known that there are five red scarves and only 2 people are telling the truth, who helped the old lady through the street?
If there are multiple answers, output in one line, separated by a space between the numbers.
For example
A B C D E (which is obviously not the correct answer)
2 Solutions
The specific code is as follows:
Importjava.util.ArrayList;Importjava.util.Collections; Public classMain { Public Staticarraylist<integer> result =NewArraylist<integer>(); Public voidGetResult (intIintj) {//Ans[i] = 0 indicates the initial state, ans[i] = 1 indicates that there must be no help//Ans[i] = 1 means there must be help, ans[i] = 2 indicates that it is possible to help int[] ans =New int[5]; //a says if(i = = 0 | | j = 0) {ans[0] = 1; ans[4] = 1; } Else{ans[0] = 2; ans[4] = 2; } //b says if(i = = 1 | | j = 1) {ans[2] = 2; if(Ans[4] = = 2) {ans[4] = 1; } Else if(Ans[4] = =-1) ans[2] = 1; } Else{ans[2] = 1; if(Ans[4] = = 2) {ans[4] = 1; ans[0] = 1; } } //c says if(i = = 2 | | j = 2) {ans[3] = 2; if(Ans[2] = = 2) ans[2] = 1; Else if(ans[2] = =-1) ans[3] = 1; } Else { if(Ans[2] = = 1) return; Elseans[2] = 1; ans[3] = 1; } //D says if(i = = 3 | | j = 3) {ans[1] = 1; if(Ans[2] = = 1) return; Else if(i = = 2 | | j = 2) {ans[2] = 1; ans[3] = 1; } Else if(i = = 1 | | j = 1) {ans[2] = 1; ans[4] = 1; } } Else{ans[1] = 2; if(Ans[2] = = 2) ans[2] = 1; Else if(ans[2] = =-1) ans[1] = 1; } //e says if(i = = 4 | | j = 4) { if(Ans[4] = = 1) return; Elseans[4] = 1; } Else { if(Ans[4] = =-1) return; Elseans[4] = 1; } intCount = 0, temp = 0; for(intt = 0;t < 5;t++) { if(Ans[t] = = 1) {Count++; Temp=T; } } if(Count = = 1) { if(!result.contains (temp)) Result.add (temp); } return; } Public Static voidMain (string[] args) {main test=NewMain (); for(inti = 0;i < 5;i++) { for(intj = i + 1;j < 5;j++) {Test.getresult (i, j); }} collections.sort (Result); for(inti = 0;i < Result.size (); i++) { Chartemp = (Char) (' A ' +Result.get (i)); System.out.print (Temp+" "); } }}
Algorithm Note _152: algorithm to improve fu Granny Cross Street (Java)