Hdu-1172-猜数字 [枚举]

来源:互联网 发布:淘宝网购物女装短裙 编辑:程序博客网 时间:2024/06/02 06:15

题目传送门


因为题目限定为4位数,所以读入所有情况,并从1000枚举到9999,判断是否有1个数满足所有条件,如果有即输出,如果有超过1个或者没有,就输出Not sure。

#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>using namespace std;struct node{    int A,B,C;} p[100];int check(int x, int y){    int num1 = 0, num2 = 0;    int book1[10], book2[10];    memset(book2,0,sizeof(book2));    memset(book1,0,sizeof(book1));    for (int i = 1; i <= 1000; i*=10)    {        int xx = x/i%10;        int yy = p[y].A/i%10;        if (xx==yy)            num1++;        book1[xx]++;        book2[yy]++;    }    for (int i = 0; i < 10; i++)    {        num2+=min(book1[i],book2[i]);    }    if (num1==p[y].C && num2==p[y].B)        return 1;    return 0;}int main(void){    int n;    while (scanf("%d", &n),n)    {        int book[10000];        memset(book,0,sizeof(book));        for (int i = 0; i < n; i++)            scanf("%d %d %d", &p[i].A, &p[i].B, &p[i].C);        int sum=0;        int num=-1;        for (int i = 1000; i<=9999; i++)        {            int f = 0;            if (!book[i])            {                for (int j = 0; j < n; j++)                {                    if (!check(i,j))                    {                        f=1;                        book[i]=1;                        break;                    }                }                if (f==0)                {                    num = i;                    sum++;                }            }        }        if (sum!=1)            printf("Not sure\n");        else            printf("%d\n",num);    }    return 0;}
0 0
原创粉丝点击