Java Fun logic algorithm

Source: Internet
Author: User

/** known 4 students in a math test 100 points, when Xiao Li asked the 4 who took 100 ticks, 4 people answered as follows: A said: Not me. b says: c C says: D. D said: He's nuts.

It is known that three people are telling the truth, and one is telling lies. Now, based on this information, find out who has 100 points.
* */

Ideas: Four people first assume that the first person scored 100 points, and then at this time to determine whether there are three people say the truth, if there is the person, in turn, to judge four people.

 Public classMain { Public Static voidMain (string[] args) {        CharThisman; CharK; intN=0;  for(k = ' A '; k<= ' D '; k++) {Thisman=K; N= (((thisman!= ' A ')? 1:0) + ((thisman== ' C ')? 1:0) + ((thisman== ' d ')? 1:0) + ((thisman!= ' d ')? 1:0));            SYSTEM.OUT.PRINTLN (n); if(n==3) {System.out.println ("100 Divided into:" +Thisman);  Break; }        }        if(k> ' D ') System.out.println ("No Solution"); }}

*/A said:-B did not steal, is Ding stole. ‖ B said:-I did not steal, is the C steal. ‖ C said:-A did not steal, is B stole. ‖ Ding said:-I did not steal ‖
* Please judge who is the thief according to the four people's talk?

Idea: A total of four people to express their views, assuming that I was a rogue, set it to 1, the remaining three people set to 0, and then determine whether four conditions are established, thus determining the number of people are thieves.

This assumes that a, B, C, D represent four people, and a variable value of 1 means that the person is a rogue.

 Public classPRA1 { Public Static voidMain (string[] args) {intArr[] =New int[4];  for(inti = 0; I < 4; i++) {//Assuming that I am a rogue             for(intj = 0; J < 4; J + +) {//set Rogue to 1, remaining set to 0                if(j==i) arr[j]=1; ElseArr[j]=0; }            if(arr[3]+arr[1]==1&&arr[1]+arr[2]==1&& arr[0]+arr[1]==1) {//determine whether the condition is established                 for(intm = 0; M < arr.length; m++) {                    if(arr[m]==1) System.out.printf ("%c", m+ ' A '); }            }        }    }}

Java Fun logic algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.