UVa340-Master-Mind Hints

xiaoxiao2021-02-28  112

Master-Mind Hints

思路:

原题英文巨长,题意大概就是先给你正确的系列,然后再给你一系列序列如果某个元素位置和值都正确A++,值正确位置不正确B++,最后输出(A,B)。A的数量很好找直接比较,问题是B不好找,要判断是否在原序列出现过,还要判断是不是多了等等。后来看了刘如佳书上的写法,比较两个序列中每个数字出现较少的次数就是对B的贡献(假设正确序列X,猜测序列Y,X中有3个3,Y有2个3那么最多也是两个在X中存在过,反过来X中只有两个3,那么Y中多出来的那个肯定是错误的。所以选择X,Y中出现次数少的),最后再减去位置正确的数量就是B的值了。

代码:

#include <bits/stdc++.h> using namespace std; int main() { int n,t=1; while(scanf("%d",&n),n) { map<int ,int>mp,weizhi; int ans,ok=0,cz=0,sum; for(int i=1;i<=n;i++) { scanf("%d",&ans); mp[ans]++; weizhi[i]=ans; } printf("Game %d:\n",t++); while(1) { sum=ok=cz=0; map<int,int>flag,num; for(int i=1;i<=n;i++) { scanf("%d",&ans); sum+=ans; if(weizhi[i]==ans) ok++; num[ans]++; } for(int i=0;i<=9;i++) cz+=min(num[i],mp[i]); if(!sum) break; else printf(" (%d,%d)\n",ok,cz-ok); } } return 0; }
转载请注明原文地址: https://www.6miu.com/read-20874.html

最新回复(0)