zoukankan      html  css  js  c++  java
  • HDU1172题解报告(水题)

    //因为数据量较小,暴力枚举就

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int maxn = 100 + 15;
    typedef struct Answer
    {
        int a;//猜测的数
        int b;//正确的数字
        int c;//数字正确的位置
    }arr[maxn];
    arr answer;
    bool Judge(const Answer& an,int local[])
    {
        bool flag_t[4],flag_l[4];
        memset(flag_l,false,sizeof(flag_l));
        memset(flag_t,false,sizeof(flag_t));
        int cnt_b = 0,cnt_c = 0;//正确数字个数,正确位置个数
        int test[4];
        int tmp = an.a;
        test[0] = tmp % 10; tmp /= 10;//
        test[1] = tmp % 10; tmp /= 10;//
        test[2] = tmp % 10; tmp /= 10;//
        test[3] = tmp;//
        for(int i=0;i!=4;++i)
        {
            if(test[i]==local[i])
            {
                ++cnt_b;
                ++cnt_c;
                flag_l[i] = true;
                flag_t[i] = true;
            }
        }//判断正确位置
        for(int i=0;i!=4;++i)
        {
            if(!flag_t[i])//未被匹配
            {
                for(int j=0;j!=4;++j)
                {
                    if(!flag_l[j]&&test[i]==local[j])
                    {
                        ++cnt_b;
                        flag_t[i] = true;
                        flag_l[j] = true;
                        break;
                    }
                }
            }
        }
        if(cnt_b==an.b&&cnt_c==an.c)
            return true;
        return false;
    }
    int main()
    {
        int n;//n个猜测
        while(cin>>n&&n)
        {
            memset(answer,0,sizeof(answer));
            for(int i=0;i!=n;++i)
                cin>>answer[i].a>>answer[i].b>>answer[i].c;//输入猜测
            int value = 0,cnt = 0;//cnt 统计符合的个数
            int local[4];
            for(int i=1000;i<=9999;++i)
            {
                int tmp = i;
                //拆分数字
                local[0] = tmp % 10; tmp /= 10;//
                local[1] = tmp % 10; tmp /= 10;//
                local[2] = tmp % 10; tmp /= 10;//
                local[3] = tmp;//
                int j;
                for( j=0;j!=n;++j)
                {
                    if(!Judge(answer[j],local))
                        break;
                }
                if(j==n)
                {
                    ++cnt;
                    value = i;
                }
            }
            if(cnt==1)
                cout<<value<<endl;
            else
                cout<<"Not sure"<<endl;
        }
    }

    完事了

    不怕万人阻挡,只怕自己投降。
  • 相关阅读:
    程序员的学习和积累
    【STL】-迭代器的用法
    【STL】-list的用法
    【STL】-deque的用法
    【STL】- vector的用法
    数据结构-各种排序算法
    【STL】-priority_queue的用法
    数据结构-二项队列
    IT公司100题-8-智力题
    IT公司100题-7-判断两个链表是否相交
  • 原文地址:https://www.cnblogs.com/newstartCY/p/11444327.html
Copyright © 2011-2022 走看看