who's lying?
Consider some of these statements:
One of four students in the West has done a good deed, asked separately, and their answers were:
-A said: Not Me
-B says: it's C.
-C says: it's d.
-D said: C nonsense
Known four people three said the truth, one said is a lie, ask who said the lie, who did the good.
Through a simple statute we can get the answer, B and C have a liar, A and D is the truth, D said: C nonsense, then C lie, C is a good person.
How do we put it to the computer? Enumeration method, called enumeration, also requires traversing in all possible spaces.
#include <iostream>
int main (int, char**)
{
char space[] = {' A ', ' B ', ' C ', ' D '};
Important. The definition of the whole solution space
int i = 0;
for (; i < 4; i++)
{
int n_honest = (Space[i]!= ' A ') + (space[i] = = ' C ') + (space[i) = = ' D ') +! ( Space[i] = = D);
if (n_honest = = 3) break
;
Std::cout << Space[i] << "Did the Good things" << Std::endl;
return 0;
}
who's the criminal suspect?
A criminal investigation brigade in a certain case involving 6 suspects is analyzed as follows: A,b at least one of them has committed a crime. A, E, F 3 people have at least two persons involved in the crime A, D can not be the co-accused B, C or at the same time, or not in the case of c,d and only one of the crime if D did not commit crime, then E can not participate
In this case, the possibility of more, artificial way may be more time-consuming, if the full use of modern computing power (hot and high frequency), will give similar problems bring new opportunities.
In the way of enumeration, the whole possible space is still judged by traversal. Known altogether six persons, each person only "is the criminal" "is not the criminal" two kinds of possibility, then 6 person composition possible number 26=64 2^6=64:
#include <iostream> #include <bitset> int main (int, char**) {bool B1, B2, B3, B4, B5, B6;
Char judge[2][9] = {"Not a criminal", "is a criminal"};
Chinese accounts for two bytes for (int i = 0; i < POW (2, 6); ++i) {std::bitset<6> bs (i); i = 0⇒[0, 0, 0, 0, 0, 0]//i = 63⇒[1, 1, 1, 1, 1, 1]//bs[0], BS [1], bs[2], bs[3], bs[4], bs[5]//A, B, C, D, E, F b1 = bs[0]| |
BS[1]; B2 = (bs[0]&&bs[4]) | | (bs[0]&&bs[5]) | |
(Bs[4]&&bs[5]); B3 =!
(Bs[0]&&bs[3]); B4 = (bs[1]&&bs[2]) | |
(!bs[1]&&!bs[2]); B5 = (bs[2]&&!bs[3]) | |
(!bs[2]&&bs[3]); B6 = bs[3]| |!
BS[4]; Non-d⇒ non-e equivalent to d| |
Non e if (B1&&B2&&B3&&B4&&B5&&B6)//each condition is satisfied {std::cout << "A:" << judge[bs[0]] << Std::endl;
Std::cout << "B:" << judge[bs[1]] << Std::endl;
Std::cout << "C:" << judge[bs[2]] << Std::endl;
Std::cout << "D:" << judge[bs[3]] << Std::endl;
Std::cout << "E:" << judge[bs[4]] << Std::endl;
Std::cout << "F:" << judge[bs[5]] << Std::endl;
Break
} return 0; }
Everyone's just half right.
For the ranking of five contestants, each of the following statements: A:b Second, I third B: I second, E four C: I first, D second d:c Finally, I third E: I four, A first
Everyone is half right, half wrong. Please program the correct name for this.
According to the arrangement of knowledge, we know five people rank, all the number of possibilities: a55=120 a_5^5=120, I did not construct a suitable for loop, control the size of the solution space, but temporarily have to use the five for the Loop +if judge:
int B1, B2, B3, B4, B5; for (int A = 1; A < 6; ++A) for (int B = 1; B < 6; ++B) for (int C = 1; C < 6; ++C) for (int D = 1; D < 6; ++D) for (int E = 1; E < 6;
++e) {//essentially expresses the concept of 5^5=3125 if (a*b*c*d*e=120)
{B1 = (b==2) + (a==3);
B2 = (b==2) + (e==4);
B3 = (c==1) + (d==2);
B4 = (c==5) + (d==3);
B5 = (e==4) + (a==1); if (b1*b2*b3*b4*b5==1) {cout << A << B << C << ;
D << E << Endl;
Break }
}
}
Later, when I run in the evening, the ugly five cycles always tortured me, finally let me think of how to use the combination of A55 a_5^5 form, the perfect solution to this problem, the following code used to Stl<algorithm> next_permutation, See the description of the function for details:
#include <iostream>
#include <algorithm> //next_permutation
#include <iterator>
int main (int, char**)
{
int ranking[] = {1, 2, 3, 4, 5};
int B1, B2, B3, B4, B5;
BOOL flag = TRUE;
while (flag)
{
B1 = (ranking[1] = = 2) + (ranking[0] = = 3);
B2 = (ranking[1] = = 2) + (ranking[3] = = 4);
B3 = (ranking[2] = = 1) + (ranking[3] = = 2);
B4 = (ranking[2] = = 5) + (ranking[3] = = 3);
B5 = (ranking[4] = = 4) + (ranking[0] = = 1);
if (b1*b2*b3*b4*b5 = = 1)
{
std::copy (ranking, ranking+5, Std::ostream_iterator (Std::cout, ""))
; Std::cout << Std::endl;
break;
Flag = std::next_permutation (ranking, ranking+5);
}
return 0;
}