Java learning journey (2): The sick dog 1 (logical deduction)

 

Preface: This article is a personal notes, for example of some code, I do not know whether it is reasonable, please advise.

 

 

  Noon to see the face questions a student, I feel it burning brain, the brain can not burn a man to burn, to burn everyone together.

  There are 50 people in the village, each person has a dog. In these 50 dogs in sick dog (the disease is not contagious), so it is necessary to identify sick dog. Everyone can observe the other 49 dogs to determine whether they are sick, only you can not see the dog. The results obtained may not be observed after the exchange, not to inform the owner of the sick dogs. Once the master calculate their own home sick dog is going to shoot his dog, but each person only the right to shoot his dog, have no right to kill other people's dogs. The first day, the next day have no shot, the third day came a burst of gunfire, Q: There are several sick dogs? How extrapolated?

  Given the cruel killing of dogs too, I changed it to hospital for treatment.

  There are 50 people in the village, each person has a dog. In these 50 dogs in sick dog (the disease is not contagious), so it is necessary to identify sick dog. Everyone can observe the other 49 dogs to determine whether they are sick, only you can not see the dog. The results obtained may not be observed after the exchange, not to inform the owner of the sick dogs. Once the master calculate their own home is sick dog going to hospital for treatment, but each person only the right to treat their dogs, have no right to treat other people's dogs. The first day, the next day no one will dog the hospital, the third day there will be a few people sent to rescue the dog, and asked: There are a few sick dog? How extrapolated?


  In fact, the beginning, how I would like to get out, then the group had a classmate who made answer.

  I found the answer I have not read!

  Gan! It's not insult my intelligence it?

  So how can I be ashamed, thinking for a long time, quite an epiphany, this record is for reference only.

  In fact, the answer to which, with the brute-force method, from a dog, the dog to n exhaustive, but is derived for each hypothesis I really do not understand (or indeed because I understand the limited ability to read) Therefore derivation, I re-thinking.

 

  First, the dog owner is a class (class Owner), the dog is a class (class Dog). Then according to casual working mean, we were to analyze the properties and methods class and Dog Owner class.

  Code is as follows :

 

public  class Owner {
     // Properties section
     // dog owners must have a dog, the dog can be checked other masters, so to public
     // But the dog sick state, dog owners can not see the 
    public Dog Dog;
     // dog owners also have a car, taken to hospital for the dog (not another car type set up, instead of using String), a private car is definitely slightly 
    private String = Koenigsegg "Koenigsegg a" ;
     // because there are a lot of masters, a single owner should have its own number, 
    public  int ownerCode;
     // find the number of other dogs in the dog's sick, because the information can not be shared, so this is private property 
    private  int sickDogRecord = 0 ;
     // own whether the dog sick, sick is true, not sick is false 
    public  boolean dogIsSick; 
    
    // methods
     //Dog owners can check other dog owners dogs 
    public  void checkDog (Dog otherOwnerDog) {
         // if the dog is sick, sickDog ++; 
        IF (dog sick) { 
            sickDogRecord ++ ; 
        } 
    } 
    // dog owners determine whether their dogs sick 
    public  void IsSick () {
        // IF ... ill if their dogs,
        // dogIsSick = to true; 
    }
     // kill a 
    public  void killDog (dog myselfDog) {
         // is invoked method hospital dogs 
        myselfDog. beSendHospital (the Koenigsegg); // pass a hospital tool
         // this.dog = NULL; 
    }
    
    public Owner(){
        
    }
    //构造方法重载
    public Owner(Dog dog, int code, boolean sick){
        this.dog = dog;
        this.ownerCode = code;
        this.dogIsSick = sick;
    }
}
class Owner

 

public  class Dog {
     // Properties section
     // disease states, there are no sick, sick is true, not sick is false 
    boolean SICK;
     // how many owners, there are that many dogs, dogs numbered 
    public  int dogCode;
     // method part
     // dog was taken to hospital 
    public  void beSendHospital () { 
        System.out.println ( the this .dogCode + "No dog was sent to the hospital." ); 
    } 
    // dogs were taken to hospital overloaded 
    public  void beSendHospital (Tool String) { 
        System.out.println ( the this .dogCode + "is a dog owner number with" + tool + "to the hospital." ); 
    } 
    public dog () {
        
    } 
    // constructor overloads 
    public Dog ( Boolean SICK, int code) {
         the this .sick = SICK; 
        dogCode = code; 
    } 
}
class Dog

 

  Next, the dog owners in logically divided into two classes, the sick dog owner class A, no disease dog owner class B.

 

Derivation start :

  According casual working " sick dog in these 50 dogs in (the disease is not contagious) " that sick dog ≥1. So from the beginning exhaustive.

  Corollary 1: The sick dog ≥1.

 

Suppose a:

  We have a dog sick.

  Then all the owner of only one A, after the A check other people's dogs, A.sickDogRecord = 0 .

At this time, for A, he is aware of Corollary 1 , and he found no other dog sick, so sick dog can only own a dog, A on the first day, it must be a dog hospital.

This is inconsistent with the casual working results.

  A hypothesis does not hold.

  Corollary 2: If only 1 dog sick, then the owner of a sick dog at day 1 will certainly dog ​​hospital treatment.

 

Assumptions b:

  There are two dogs sick.

  So there are two objects A class, first A1 reasoning.

  After checking other dogs A1, A.sickDogRecord =. 1 , there are two A1 will be determined:

  ①: My dog ​​does not get sick

  A1认定自己的狗没病,但是他又看到了1只病狗,A1会认为,所有的狗狗里只有1只病狗。

  此时对于A1来说,根据题干“每个人可以观察其他的49条狗,以判断它们是否生病,只有自己的狗不能看”,他会觉得病狗的主人A2看到的狗全是健康的,于是会根据推论一推论二在第一天就将狗狗送医,但是第一天什么都没发生。

  判断①不成立。

  ②:我的狗有病

  判断1不成立,但是A1确实是看到了1条病狗,第一天又没有人送医,那唯一可以解释的就是A1看到的病狗的主人A2也看到了其他1条病狗,并且也觉得自己的狗不是病狗,所以A2没有在第一天就将狗狗送医治疗,于是在第二天,A1明白了一个道理:“我看到了一条病狗,这条病狗属于一个我不认识的人,暂时用X(A2)代替,而X不能看自己的狗,同时看到了另外一条病狗,那么X看到的只有自己这条自己看不到的狗,那么我的狗也是病狗!”于是,A1的狗有病,同时A2也以相同的推理判断出,他自己的狗也是病狗,于是,第二天,A1和A2同时将狗送医去治疗。

  但是,第二天实际上什么也没有发生!

  假设b不成立!

  推论3:若只有2条狗生病,那么病狗的主人在第2天一定会将狗送医治疗。

 

假设c:

  有三条狗生病

  那么A类的对象就有三个,继续对A1进行推理。

  A1在检查其他狗狗之后,A.sickDogRecord = 2,A1将会有两种判断:

 

  ①:A1的狗没有病

 

  A1认定自己的狗没病,但是他又看到了2只病狗,A1会认为,所有的狗狗里只有2只病狗。

 

  此时对于A1来说,根据题干“每个人可以观察其他的49条狗,以判断它们是否生病,只有自己的狗不能看”,他会觉得病狗的主人A2和A3各自能看到的只有一只病狗,于是会根据推论一、推论二推论三,在第二天就将狗狗送医,但是第二天也什么都没发生。

 

  判断①不成立。

 

  ②:A1的狗有病

  判断1不成立,但是A1确实是看到了2条病狗,第二天也没有人送医,那唯一可以解释的就是A1看到的病狗的主人A2和A3也同时看到了其他2条病狗,并且也觉得自己的狗不是病狗,所以A2和A3没有在第二天就将狗狗送医治疗,于是在第三天,A1明白了一个道理:“我看到了2条病狗,这2条病狗属于2个我不认识的人,暂时用X(A2)和Y(A3)代替,而X和Y不能看自己的狗,同时他们也各自看到了另外2条病狗,X看到的是Y的病狗和另外一条病狗z,Y看到的是X的病狗和病狗z,而我看到的只有X和Y的病狗没有其他人的病狗,并且我看不到自己的狗,那么我的狗肯定就是那条x,也就是说我的狗也是病狗!”于是,A1的狗有病,同时A2和A3也以相同的推理判断出,他自己的狗也是病狗,于是,第三天,A1、A2和A3同时将狗送医去治疗。

  假设c成立!答案就是病狗有3条。

  推论4:若只有3条狗生病,那么病狗的主人在第3天一定会将狗送医治疗。

  综上所述,依次类推,最后可以得到结论:

  有n条狗生病,那么病狗的主人就会在第n天将狗送医治疗!

 

  整个逻辑推理已经结束,但是我最想做的事还没有做。

  接下里的目标就是,将题目例化成java类,通过设计类属性和方法,类之间的关系,实现整个题目的逻辑推理和元素关系,敬请期待!

 

Guess you like

Origin www.cnblogs.com/Azir-s-soldier/p/11461976.html