一个逻辑问题_张村说谎李村不说谎

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aotian16/article/details/8243752

在论坛里看到一个逻辑题, 回答的人很多, 自己也试了下.

挺有意思的.

问题如下,

有两个村,张村和李村
张村的人星期一三五说谎
李村的人星期二四六说谎

一人问,今天周几?结果两个村的人都回答“前天是我说谎的日子”

问:今天是周几?

代码如下

public class TestLie {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday",
				"Friday", "Saturday", "Sunday"};
		boolean[] lieArray_zhang = {true, false, true, false, true, false, true};
		boolean[] lieArray_li = {false, true, false, true, false, true, true};
		Person zhang3 = new Person("zhang3", lieArray_zhang);
		Person li4 = new Person("li4   ", lieArray_li);

		for (int i = 0; i < 7; i++) {
			System.out.println("Today is " + days[i]);
			zhang3.say(i);
			li4.say(i);
		}

	}

}

class Person {
	String name;
	boolean[] lieArray;

	public Person(String name, boolean[] lieArray) {
		this.name = name;
		this.lieArray = lieArray;
	}

	public void say(int day) {
		int f = (day + 5) % 7;
		if (lieArray[day]) {
			// today is lie day
			if (lieArray[f]) {
				System.out.println("    " + name
						+ " : The day before yesterday I not lay.");
			} else {
				System.out.println("    " + name
						+ " : The day before yesterday I lay.");
			}
		} else {
			// today is not lie day
			if (lieArray[f]) {
				System.out.println("    " + name
						+ " : The day before yesterday I lay.");
			} else {
				System.out.println("    " + name
						+ " : The day before yesterday I not lay.");
			}
		}
	}
}

其中周日没有特别说明是都说谎还是都不说谎, 但是对结果没有影响

可以修改lieArray的最后一个为false试试看

以上代码结果如下

Today is Monday
    zhang3 : The day before yesterday I lay.
    li4    : The day before yesterday I lay.
Today is Tuesday
    zhang3 : The day before yesterday I lay.
    li4    : The day before yesterday I not lay.
Today is Wednesday
    zhang3 : The day before yesterday I not lay.
    li4    : The day before yesterday I not lay.
Today is Thursday
    zhang3 : The day before yesterday I not lay.
    li4    : The day before yesterday I not lay.
Today is Friday
    zhang3 : The day before yesterday I not lay.
    li4    : The day before yesterday I not lay.
Today is Saturday
    zhang3 : The day before yesterday I not lay.
    li4    : The day before yesterday I not lay.
Today is Sunday
    zhang3 : The day before yesterday I not lay.
    li4    : The day before yesterday I lay.


论坛地址

http://bbs.csdn.net/topics/390294090

展开阅读全文

没有更多推荐了,返回首页