Digital logic to resolve issues of information sharing

2010-06-21  来源:本站原创  分类:Java  人气:148 

1. Preconditions:

The logical subject of the information in the digital description used.

2. Example:

(1) Example 1:

Problem Description: Police arrested a, b, c, d four theft suspects, of whom only one is a thief. Interrogation results are as follows.

a said: "I am not a thief."
b said: "c is a thief."
c said: "The thief is certainly d."
d said: "c in the innocent people."

Now know of 3 people 4 people say is the truth, one that is false, and asked in the end who is the thief?

Analysis: the a, b, c, d four numbered, respectively 1,2,3,4.
Store thief with the number x, then x's value range from 1 to 4.
4 individual words can be written, respectively:
a say: x! = 1
b say: x == 3
c say: x == 4
d say: x! = 4

Code:

package boke.written;

public class InfoToNumber {

        /**
         * @param args
         */
        public static void main(String[] args) {
                int x;

                for (x = 1; x <= 4; x++) {
                        int s = 0;

                        if (x != 1) {
                                s++;
                        }

                        if (x == 3) {
                                s++;
                        }

                        if (x == 4) {
                                s++;
                        }

                        if (x != 4) {
                                s++;
                        }

                        if (s == 3) {
                                char ch = (char) (64+x);
                                System.out.println(ch + " Is the thief  ");
                        }
                }

        }
}

     Output results  :C Is the thief

(2) Example 2:

Problem Description: 3 teachers on a number of school competitions were predicted. They predict the following results.
A wrote: A number one student, student B was third.
B wrote: Student C was the first student D was fourth.
C wrote: D was the second student, student A was third.

Competition results show that they say on the half wrong half, and no tied rankings, find the A, B, C, D their ranking.


Analysis: The numbers 1,2,3,4 to represent the students A, B, C, D be the ranking.
I say: (A == 1) + (B == 3) = 1;
Have to say: (C == 1) + (D == 4) = 1;
C say: (D == 2) + (A == 3) = 1;
Since no parallel rank, the rank of four and is equal to 10 = 1 + 2 + 3 + 4.

Code:

public class InfoToNumber2 {

        /**
         * @param args
         */
        public static void main(String[] args) {
                int A,B,C,D;

                for (A = 1; A <= 4; A++) {
                        for (B = 1; B <= 4; B++) {
                                if (A != B) {
                                        for (C = 1; C <= 4; C++) {
                                                if ((C != A) && (C != B)) {
                                                        D = 10 - A - B -C;
                                                        if ((D != A) && (D != B) && (D != C)) {
                                                                boolean bool1 = false;
                                                                boolean bool2 = false;
                                                                boolean bool3 = false;

                                                                if ((A == 1) || (B == 3)) {
                                                                        bool1 = true;
                                                                }

                                                                if ((C == 1) || (D == 4)) {
                                                                        bool2 = true;
                                                                }

                                                                if ((D == 2) || (A == 3)) {
                                                                        bool3 = true;
                                                                }

                                                                if (bool1 == true && bool2 == true && bool3 == true) {
                                                                        System.out.println("A = " + A + " B = " + B + " C = " + C + " D = " + D);
                                                                }

                                                        }
                                                }
                                        }
                                }
                        }
                }
        }
}

  Output results  :A = 4 B = 3 C = 1 D = 2
相关文章