A logical question (today's Day of the week) causes the thought

Source: Internet
Author: User

The topic is as follows, the code is as follows, hope each road great God generous enlighten.

Somewhere there are two strange villages, Zhang Zhuang people in Wednesdays, five lies, Li people in Tuesday, six lies. On other days they tell the truth.

One day, the field of Wang Congming came here, met two people, respectively, to their questions about the date. Two people said: "The day before yesterday is my days of lying." ”

If the two people asked were from Zhang Zhuang and Li Village, what day is it?

The following is a way to traverse, self-feeling is not very good, do not know whether the logical derivation of the way to obtain the final result.

1  Packagejiang.test;2 /**3 * Somewhere there are two strange villages, the people of Zhang Zhuang in Wednesdays, five lies, Li people in Tuesday, six lies. On other days they tell the truth. 4 * One day, the foreign Wang Congming came here and met two people, and asked them about the date respectively. Two people said: "The day before yesterday is my days of lying." "5 * If the two people asked were from Zhang Zhuang and Li Village, what day is it? 6  * @authorAdministrator7  *8  */9  Public classWhatday {Ten  One      Public Static voidMain (string[] args) { A         BooleanIsLay =true; -          for(intBeyes = 1;beyes <= 7;beyes++){ -             intToday = (beyes+2)%7; theString Zhangzhang = "Zhang"; -String licun = "Li"; -             if(Isright (Zhangzhang,beyes,islay) &&isright (Licun,beyes,islay)) { -SYSTEM.ERR.PRINTLN ("This day is the week" +today); +             } -         } +     } A     /** at * Logic is correct, return true -      * @paramUser -      * @paramBeyes -      * @paramIsLay -      * @return -      */ in      Public Static BooleanIsright (String user,intBeyes,BooleanIsLay) { -         intToday = (beyes+2)%7; to         BooleanBelay =islying (user,beyes); +         BooleanLay =islying (user,today); -         if(lay&&!belay==IsLay) { the             return true; *}Else if(!lay&&belay==IsLay) { $             return true;Panax Notoginseng}Else  -             return false; the     } +     /** A * Whether to lie the      * @paramUser +      * @param Day -      * @return $      */ $      Public Static BooleanIslying (String user,intDay ) { -         if(User.equals ("Zhang")){ -             if(day==1| | day==3| | Day==5) the                 return true; -}Else if(User.equals ("Li")){Wuyi             if(day==2| | day==4| | Day==6) the                 return true; -         } Wu         return false; -     } About}

A logical question (today's Day of the week) causes the thought

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.