作者:国国国国涛 | 来源:互联网 | 2023-10-10 10:29
#include<iostream>usingnamespacestd;*四位同学中有一位是做了好事,不留名,其中,A说:不是我B说:是CC说:是DD反驳:他胡说已知3人说的是
#include
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 {
int count = 0;
char thisMan = 'A' + i;
for (int j = 0; 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 " < }
else
{
cout <<"出错!!!!!!" < }
}
int main()
{
FindThisMan();
system("pause");
return 0;
}