水题------找出正确顺序对的数和正确但顺序不对的数

题目链接:https://vjudge.net/problem/UVA-340#author=0

题意:关键句子:In this problem you will be given a secret codes1: : : snand a guessg1: : : gn, and are to determinethe hint. A hint consists of a pair of numbers determined as follows.Amatchis a pair(i; j),1inand1jn, such thatsi=gj. Match(i; j)is calledstrongwheni=j, and is calledweakotherwise. Two matches(i; j)and(p; q)are calledindependentwheni=pif and only ifj=q. A set of matches is calledindependentwhen all of its members are pairwiseindependent.

可以得出输出坐标中x为正确且位置对的数,y为正确但位置不对的数(注:由样例可以得出y为除去x之外的数)

解析:1.该题的思路是桶排序,定义两个数组统计出现次数,删去x的数字的个数,然后当两个数组都不为0时加上最小的数

   2.该秒的这个题,但是由于每次样例都应该重置次数,即需要初始化浪费了许多时间

源代码:#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1100],b[1100],c[10],d[10],e[10];
int main()
{
    int n,ans1,ans2,cou=0;
    while(cin>>n&&n)
    {
        cou++;
        memset(c,0,sizeof(c));
        for(int i=1; i<=n; i++)
        {
            cin>>a[i];
            c[a[i]]++;
        }
        printf("Game %d:\n",cou);
        for(;;)
        {
            memset(d,0,sizeof(d));
            memcpy(e,c,sizeof(c));
            ans1=0;
            ans2=0;
            for(int i=1; i<=n; i++)
            {
                cin>>b[i];
                d[b[i]]++;
            }
            if(b[1]==0)
                break;
            for(int i=1; i<=n; i++)
                if(a[i]==b[i])
                {
                    ans1++;
                    d[a[i]]--;
                    e[a[i]]--;
                }
            for(int i=1; i<=10; i++)
            {
                if(d[i]>0&&e[i]>0)
                {
                    ans2+=min(d[i],e[i]);
                }
            }
            printf("    (%d,%d)\n",ans1,ans2);
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Joe2019/p/12660174.html
今日推荐