Joke Collection Website - Cold jokes - Whoever can do this question is awesome!
Whoever can do this question is awesome!
There are five houses, each house has a different color, and people from different countries live in them. Everyone has their own
raised different pets, likes and dislikes. Drinks, smoke different brands of cigarettes. The following information is now known
Englishmen live in red houses.
The Spanish (spaniard) raised a dog (dog).
Norwegians live in the first house on the left.
People in the yellow house like to smoke kools brand cigarettes.
The man who smokes Chesterfields and the man who keeps foxes are neighbors.
Norwegians live next to blue houses.
The man who smokes Winston cigarettes keeps a snail.
People who smoke Lucky Strike cigarettes like to drink orange juice.
Ukrainians like to drink tea.
Japanese smoke parliament brand cigarettes.
The man who smokes Kools cigarettes and the man who raises horses are neighbors.
People who like to drink coffee live in green houses.
The green house is to the right of the ivory house (right side in the picture).
The people in the middle house like to drink milk.
Based on the above conditions, can you tell me which house has people raising zebras, and which house has people who
like to drink water? Or can you get everything to fall into place?
This is a typical conditional problem. According to each condition, we can exclude some situations until we finally find the answer.
However, because there are too many conditions for this problem, it will take a lot of time to answer it manually.
We might as well use this time to write a program and let the computer answer the question. Really, programming must take less time than manual calculation!
To use Prolog to solve this type of problem, the "select and then verify" method is generally used. That is, a certain method is used to propose a series of possible solutions, and then the verification part is used to judge whether the solution meets the meaning of the problem. Until you find the answer
.
Let’s first analyze how many possible solutions there are.
Each house has a different color, so there are 5 in terms of color! A person has five characteristics: color, nationality, pets, cigarettes and drinks. So there are 5!*5 situations in one ***, that is, 600 situations. There are not many.
You can use a program to enumerate them all.
The following describes how to use Prolog to write a solution program.
In this program, the structure h(C, N, P, Y, D) is used to store room information. C, N, P, Y, and D respectively correspond to color,
nationality, pets, cigarettes and drinks. Since there are five rooms, use a list to store information for all rooms
. The list is:
).
X = 5;
X = 3;
no
Obviously, next finds the element adjacent to 4 in the list.
The elements of the list here are simple numbers
If it is the structure h (C, N, P, Y, D) that represents the room information mentioned above, its function is also the same
As mentioned before, we will use the "select and then verify" method to find out the answer, so how do we choose?
? The member/2 predicate introduced previously will be used here. Do you still remember the definition of member/2?
member(A,).
yes
We once said that Prolog predicates can be used in many ways, so member/2 can also be used to traverse
All elements of the list. -member(X,).
X = 1;
X = 2;
X = 3;
X = 4 ;
X = 5 ;
no
This is exactly the function we need, using it we can select all situations.
With the above preparations, we can start to formally write the answer part. Let's list the programs first
and then explain them. The predicate to solve the problem is solve/3. The first parameter A room for people who drink water
.
solve(X, TT, TTT):-
First bind X as a room list. Note that the attributes of the room cannot be determined at this time, so variables are used
p>
represents.
(C4, N4, P4,
Y4, D4), h(C5, N5, P5, Y5, D5)],
The British (englishman) live in red (red ) house.
member(Z1, X), first select a room Z1 from the X list,
color(Z1, red), the color of Z1 is red.
Nation (Z1, englishman), the person living in Z1 is englishman. The same below.
The Spanish (spaniard) raised a dog (dog).
member(Z2, X),
pet(Z2, dog),
nation(Z2, spaniard),
Norway People (norwegians) live in the first house on the left.
first(Z3,
member(Z4, X),
yan(Z4, kools),
color(Z4, yellow),
The man who sells Chesterfields cigarettes lives next to the man who keeps foxes.
member(Z5, X),
pet(Z5, fox),
next(Z6, Z5, X), use next(Z5, Z6 , X) as well.
yan (Z6, chesterfields),
Norwegian lives next to the blue house.
member(Z7, X),
color(Z7, blue),
next(Z8, Z7, X),
nation (Z8, norwegian),
The person who smokes winston cigarettes raises a snail (Snails).
member(Z9, X),
yan(Z9, winston),
pet(Z9, snails),
People who smoke Lucky Strike cigarettes like to drink orange juice.
member(Z10, p>
Ukrainians like to drink tea.
member(Z11, X),
nation(Z11, ukrainian),
drink(Z11, tea),
Japan People (Japanese) smoke parliaments brand cigarettes.
member(Z12, X),
nation(Z12, japanese),
yan(Z12, parliaments),
The man who sells Kools cigarettes is the neighbor of the man who raises horses.
member(Z13, X),
pet(Z13, horse),
next(Z14, Z13, X),
yan(Z14, kools),
People who like to drink coffee live in green houses.
member(Z15, X),
color(Z15, green),
drink(Z15, coffee),
green The (green) house is to the right of the ivory (ivory) house (right in the picture).
member(Z16, X),
color(Z16, ivory),
next(Z17, Z16, condition, but assume that they are neighbors, so the final answer is two.
color(Z17, green), please modify this point yourself. Of course, you also need to write a
predicate to judge the right side.
The people in the middle house like to drink milk.
middle(Z18,
Find the room where the pet is Zebra.
member(TT, X),
pet(TT, zebra),
Find the drinking room.
member(TTT, X),
drink(TTT, water).
You should have no problem reading this program. It simply translates our conditions directly into
Prolog language. For example:
A person who smokes Chesterfields cigarettes and a person who raises foxes are neighbors.
member(Z5, X),
pet(Z5, fox),
next(Z6, Z5, X), use next(Z5, Z6 , X) as well.
yan (Z6, chesterfields),
To describe it in language is: first Z5 is a house, corresponding to member (Z5, X); then its pet is fox
, corresponding to pet(Z5, fox); its neighbor is Z6, corresponding to next(Z6, Z5, , chesterfields). You see, as long as the original conditions are slightly decomposed
, it becomes our Prolog program.
Haha, anyone can program such a program. You can see the advantages of Prolog. Prolog is a descriptive language. You only need to use Prolog language to describe the problem once, and let the computer do the rest:). If you use other languages, such as C, Basic, etc., you have to consider the program flow yourself
, so these languages ??are called procedural languages. It is much lower level than Prolog.
Okay, finally let us run the program. - solve(X, TT, TTT).
X = [h(yellow, norwegian, fox, kools, water), h(blue, ukrainian, horse,
chesterfields, tea),
h(red, englishman, snails, winston, milk), h(iory, spaniard, dog, 'Lucky
Strike', 'orange juice'),
h(green, japanese, zebra, parliaments, coffee)]
TT = h(green, japanese, zebra, parliaments, coffee)
TTT = h (yellow, norwegian, fox, kools, water);
X = [h(yellow, norwegian, fox, kools, water), h(blue, ukrainian, horse,
chesterfields, tea),
h (red, englishman, snails, winston, milk), h (green, japanese, zebra,
parliaments, coffee),
h(ivory, spaniard, dog, 'Lucky Strike', 'orange juice')]
TT = h(green, japanese, zebra, parliaments, coffee)
TTT = h(yellow, norwegian, fox, kools, water)
no
Since we did not use the right-hand restriction in the question in the 13th condition, there are two answers , you can
see that the last two rooms of these two answers are exactly reversed.
If you put a little more effort into writing the answers more beautifully, this program will be perfect.
- Previous article:How to overcome the fear of speaking on stage
- Next article:Who is the ending of the movie version of The Golden Wolf?
- Related articles
- Exaggerated and funny advertising words
- To like someone is to play tricks on him.
- Funeral parlors require undergraduates majoring in economics to recruit fire chemicals. Why is "radish recruitment" repeatedly banned?
- Cao Cao is suspicious by nature. Why are so many generals willing to take refuge in him?
- People who laugh at others are always laughed at by others.
- How to write the fifth grade weekly diary?
- Small rules of family jokes
- If you want to eat seafood when traveling in Yantai, go to this market. The seafood will be different after walking around.
- What is the rain of horse-drawn carriage corrosion in 100 thousand cold jokes? I won't take it when I read comics. Please explain it to the Great God.
- What's the messy "one" sound?