zoukankan      html  css  js  c++  java
  • 找出谁是做好事的人

    #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 '';
    }
    void FindThisMan()
    {
    Student student[] = {Student(false, 'A'), Student(true, 'C'), Student(true, 'D'), Student(false, 'D')};
    char thisMan = solve(4, 3, student);
    if (thisMan != '')
    {
    cout << "It is " << thisMan << endl;
    }
    else
    {
    cout << "出错!!!!!!" << endl;
    }
    }
    int main()
    {
    FindThisMan();
    system("pause");
    return 0;
    }

  • 相关阅读:
    NSThread 多线程 三种方式
    CABasicAnimation 核心动画
    图片圆角属性
    GCD
    IOS 推送
    IOS 截图
    UIImage 截图
    UIImageView 动画
    AFN 判断网络状态
    Template 模式
  • 原文地址:https://www.cnblogs.com/mengjuanjuan/p/9877897.html
Copyright © 2011-2022 走看看