C++ 算法提高 扶老奶奶过街

法一 逻辑解法 通过直接计算 知道答案 然后输出

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
main()
{
    cout<<"A B E";
}

法二 列举法(分别假设每个人 成立的时候)

 

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
void memset(int a[],int n)
{
    for(int i=0;i<n;i++)
    {
        a[i]=0;
    }
    return;
}
main()
{
    int a[5]={0},count=0;
    char b;
    for(int i=0;i<5;i++)
    {
        memset(a,5);
        count=0;
        a[i]=1;//当它成立 
        if(a[0]!=1&&a[4]!=1) count++;//A说法
        if(a[2]==1||a[4]==1) count++;//B说法
        if(a[2]==1||a[3]==1) count++;//C说法
        if(a[1]!=1&&a[2]!=1) count++;//D说法
        if(a[4]!=1) count++;//E说法
        if(count==2)//有两个说了真话 
        {
            b='A'+i;
            cout<<b<<" ";
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_47991812/article/details/108034112