The original address: https://xeblog.cn/articles/11

Once upon a time, on the other side of the mountain, on the other side of the sea, there was a group of blue… Well, no, there are two villages, one called Zhang village and the other called Li Village. The people of the two villages conspired to deceive outsiders. The people of Zhang lied on Mondays, Wednesdays and Fridays, the people of Li lied on Tuesdays, Thursdays and Saturdays, and told the truth on all other days. One day, a foreigner came and asked what day is it today? The people of both villages answered, “The day before yesterday was the day when I lied.”

The above content is pure fiction!!

Get down to business…

The original questions are from the MBA entrance exam questions

Way of thinking (The way of thinking)

The first thing we can be sure of is this: if Zhang and Li lie today, Zhang and Li must not lie the day before yesterday. On the other hand, if Zhang and Li do not lie today, Zhang and Li must have lied the day before yesterday.

So, I just need to know whether Zhang and Li lied today and the day before yesterday to get the answer.

Without further ado, let’s get to the code

/** ** Whether zhang Cun lied * @param day * @return trueTelling a lie,false*/ private static Boolean zhangIsLie (int day) {return7! = day && day % 2 ! = 0; } /** * If li Cun is lying * @param day * @return trueTelling a lie,false*/ private static Boolean liIsLie (int day) {return day % 2 == 0;
    }
Copy the code

Here’s why Zhang village is 7! = day && day % 2 ! = 0 day % 2 == 0

Because zhang cun is 1, 3, and 5, it’s not a lie, it’s an odd number, it’s not divisible by 2, so the remainder is not 0. Sunday (7) is also odd, but they don’t lie on Sunday, so 7 needs to be judged separately. The same goes for Li Cun.

Now we can see if Zhang and Li are lying one day.

We can write another method that gets the day of the week the day before yesterday

/** * get the day before yesterday * @param day * @return
    */
   private static int beforeYesterday (int day) {
       return day > 2 ? day - 2 : day + 5;
   }
Copy the code

Now that the method is almost done, all that’s left is the logical assembly

public static void main(String[] args) { boolean zhang, li, zhangBy, liBy; int by; // loop oncefor(int i = 1; i < 8; I ++) {// get the day of the day beforeYesterday by = beforeYesterday(I); // Zhang = zhangIsLie(I); li = liIsLie(i); // If zhangBy = zhangIsLie(by); liBy = liIsLie(by); System.out.println("===== This week" + i + "& the week before yesterday" + by +"= = = = =");
            System.out.println("Today -> Zhang:" + zhang + " 李:" + li);
            System.out.println(The day before yesterday -> Zhang: + zhangBy + " 李:" + liBy);
            System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = ="); /* If Zhang and Li lie today, Zhang and Li must not lie the day before yesterday. On the other hand, if Zhang and Li do not lie today, Zhang and Li must have lied the day before yesterday. * /if((zhang && ! zhangBy || ! zhang && zhangBy) && (li && ! liBy || ! li && liBy)) { System.out.println("Today is Sunday."+ i); }}}Copy the code

Code run result

The final answer was: Monday.

conclusion

Code may need to be further optimized, clear thinking is the most important, thank you!