找出谁是做好事的人
2018-10-30 17:01 mengjuanjuan1994 阅读(484) 评论(0) 编辑 收藏 举报#include <iostream>
using namespace std;
/*
四位同学中有一位是做了好事,不留名,其中,
A说:不是我
B说:是C
C说:是D
D反驳:他胡说
已知3人说的是真话,编程序找出谁做了好事儿
*/
class Student
{
public:
Student(bool isEqual, char theMan): _theMan(theMan), _isEqual(isEqual)
{
}
bool operator()(char thisMan)
{
return _isEqual ? (thisMan == _theMan): (thisMan != _theMan);
}
private:
char _theMan;
bool _isEqual;
};
char solve(int number, int correct, Student* students)
{
for (int i = 0; i < number; i++)
{
int count = 0;
char thisMan = 'A' + i;
for (int j = 0; j < number; j++)
{
count += students[j](thisMan);
}
if (count == correct)
{
return thisMan;
}
}
return '\0';
}
void FindThisMan()
{
Student student[] = {Student(false, 'A'), Student(true, 'C'), Student(true, 'D'), Student(false, 'D')};
char thisMan = solve(4, 3, student);
if (thisMan != '\0')
{
cout << "It is " << thisMan << endl;
}
else
{
cout << "出错!!!!!!" << endl;
}
}
int main()
{
FindThisMan();
system("pause");
return 0;
}