题目1486:False coin:考虑情况问题

题目1486:False coin

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:282

解决:42

题目描述:

The "Gold Bar"bank received information from reliable sources that in their last group of N coins exactly one coin is false and differs in weight from other coins (while all other coins are equal in weight). After the economic crisis they have only a simple balance available (like one in the picture). Using this balance, one is able to determine if the weight of objects in the left pan is less than, greater than, or equal to the weight of objects in the right pan.
In order to detect the false coin the bank employees numbered all coins by the integers from 1 to N, thus assigning each coin a unique integer identifier. After that they began to weight various groups of coins by placing equal numbers of coins in the left pan and in the right pan. The identifiers of coins and the results of the weightings were carefully recorded.
You are to write a program that will help the bank employees to determine the identifier of the false coin using the results of these weightings.

输入:

The first line of the input file contains two integers N and K, separated by spaces, where N is the number of coins (2<=N<=1000 ) and K is the number of weightings fulfilled (1<=K<=100). The following 2K lines describe all weightings. Two consecutive lines describe each weighting. The first of them starts with a number Pi (1<=Pi<=N/2), representing the number of coins placed in the left and in the right pans, followed by Pi identifiers of coins placed in the left pan and Pi identifiers of coins placed in the right pan. All numbers are separated by spaces. The second line contains one of the following characters: '<', '>', or '='. It represents the result of the weighting:
'<' means that the weight of coins in the left pan is less than the weight of coins in the right pan,
'>' means that the weight of coins in the left pan is greater than the weight of coins in the right pan,
'=' means that the weight of coins in the left pan is equal to the weight of coins in the right pan.

输出:

Write to the output file the identifier of the false coin or 0, if it cannot be found by the results of the given weightings.

样例输入:
5 3
2 1 2 3 4
<
1 1 4
=
1 2 5
=
样例输出:
3
来源:
2012年北京大学计算机研究生机试真题
在此想非常深沉的吐一下槽,被九度OJ的此题目虐了好久,至今不能AC,全是wrong。
但是相同的题目,在POJ和百练OJ上都能AC,我只能说,九度OJ的测试用例真的很全面,至少有我没有想到的情况存在。。。。。。
一开始的烂代码是这样,能AC:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 1000
int n,ci,a[1005],g;
char x;
int tem[1005];
int main()
{
    int i,j;
    int k;
    int p,q;
    while(scanf("%d%d",&n,&ci)!=EOF)
    {
        for(i=0;i<=n;i++)
            a[i]=MAXN;
        k=0;
        for(i=0;i<ci;i++)
        {
            scanf("%d",&g);
            for(j=0;j<2*g;j++)
            {
                scanf("%d",&tem[j]);
            }
            getchar();
            scanf("%c",&x);
            getchar();
            switch(x)
            {
            case'=':for(j=0;j<2*g;j++)
                    {
                        a[tem[j]]=0;
                    }
                    break;
            case'<':
                for(j=0;j<g;j++)
                {
                    switch(a[tem[j]])
                    {
                    case MAXN:a[tem[j]]=-1;
                        break;
                    case 1:a[tem[j]]=0;
                        break;
                    case 0:break;
                    case -1:
                        a[tem[j]]=-2;
                        break;
                    default:break;
                    };
                }
                if(k)
                {
                    for(j=0;j<=n;j++)
                    {
                        if(a[j]<0)
                            a[j]++;
                    }
                }
                for(j=g;j<2*g;j++)
                {
                    switch(a[tem[j]])
                    {
                    case MAXN:a[tem[j]]=1;
                        break;
                    case 1:a[tem[j]]=2;
                        break;
                    case 0:break;
                    case -1:
                        a[tem[j]]=0;
                        break;
                    default:break;
                    };
                }
                if(k)
                {
                    for(j=0;j<=n;j++)
                    {
                        if(a[j]>0&&a[j]!=MAXN)
                            a[j]--;
                    }
                }
                k=1;
                for(j=1;j<=n;j++)
                {
                    if(a[j]==MAXN)
                        a[j]=0;
                }
                break;
            case'>':
                for(j=0;j<g;j++)
                {
                    switch(a[tem[j]])
                    {
                    case MAXN:a[tem[j]]=1;
                        break;
                    case 1:a[tem[j]]=2;
                        break;
                    case 0:break;
                    case -1:
                        a[tem[j]]=0;
                        break;
                    default:break;
                    };
                }
                if(k)
                {
                    for(j=0;j<=n;j++)
                    {
                        if(a[j]>0&&a[j]!=MAXN)
                            a[j]--;
                    }
                }
                for(j=g;j<2*g;j++)
                {
                    switch(a[tem[j]])
                    {
                    case MAXN:a[tem[j]]=-1;
                        break;
                    case 1:a[tem[j]]=0;
                        break;
                    case 0:break;
                    case -1:
                        a[tem[j]]=-2;
                        break;
                    default:break;
                    };
                }
                if(k)
                {
                    for(j=0;j<=n;j++)
                    {
                        if(a[j]<0)
                            a[j]++;
                    }
                }
                k=1;
                for(j=1;j<=n;j++)
                {
                    if(a[j]==MAXN)
                        a[j]=0;
                }
                break;
            default:break;
            };
        }
        k=0;
        p=q=0;
        for(i=1;i<=n;i++)
        {
            if(a[i]==MAXN)
                k++;
            if(a[i]==-1)
                p++;
            if(a[i]==1)
                q++;
        }
        if(p==1&&q!=1)
        {
            for(i=1;i<=n;i++)
                if(a[i]==-1)
                {
                    j=i;
                    printf("%d\n",j);
                    break;
                }
        }
        else
        {
            if(p!=1&&q==1)
            {
                for(i=1;i<=n;i++)
                    if(a[i]==1)
                    {
                        j=i;
                        printf("%d\n",j);
                        break;
                    }
            }
            else
            {
                if(k==1&&p==0&&q==0)
                {
                    for(i=1;i<=n;i++)
                        if(a[i])
                        {
                            j=i;
                            printf("%d\n",j);
                            break;
                        }
                }
                else
                    printf("0\n");
 
            }
        }
    }
    return 0;
}

这个代码花了我好长时间,因为难以调试,但是也补充了我所想到的所有情况。在百练OJ上 0ms AC掉。在POJ上32ms AC掉。在九度上wrong。
还有一个是后来不甘心,重写的易于调试的代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 100000
int n,k,num,s[1005],ta[505],tb[505];
int cha;
char pan;
void input()
{
    int i,j;
    int tem;
    scanf("%d",&num);
    for(i=0;i<num;i++)
        scanf("%d",&ta[i]);
    for(i=0;i<num;i++)
        scanf("%d",&tb[i]);
    getchar();
    scanf("%c",&pan);
    getchar();
    if(pan=='>')
        for(i=0;i<num;i++)
        {
            tem=ta[i];
            ta[i]=tb[i];
            tb[i]=tem;
        }
}
void gengxin()
{
    int i,j;
    if(pan=='=')
    {
        for(i=0;i<num;i++)
            s[ta[i]-1]=s[tb[i]-1]=0;
    }
    else
    {
        for(i=0;i<num;i++)
        {
            if(s[ta[i]-1]>0)
            {
                if(s[ta[i]-1]!=MAXN)
                    s[ta[i]-1]=0;
                else
                    s[ta[i]-1]=-1;
            }
            else
            {
                if(s[ta[i]-1]<0)
                    s[ta[i]-1]--;
            }
            if(s[tb[i]-1]<0)
                s[tb[i]-1]=0;
            else
            {
                if(s[tb[i]-1]>0)
                {
                    if(s[tb[i]-1]==MAXN)
                        s[tb[i]-1]=1;
                    else
                        s[tb[i]-1]++;
                }
            }
        }
        cha++;
    }
}
void print()
{
    int i,j;
    int p,q,r;
    int h,k,f;
    h=k=f=0;
    for(i=0;i<n;i++)
        if(s[i])
        {
            if(s[i]==cha)
            {
                h++;
                p=i;
            }
            if(s[i]==cha*(-1))
            {
                k++;
                q=i;
            }
            if(s[i]==MAXN)
            {
                f++;
                r=i;
            }
        }
    if(cha)
    {
        if(h==1&&k!=1)
            printf("%d\n",p+1);
        else
            if(k==1&&h!=1)
                printf("%d\n",q+1);
            else
                printf("0\n");
    }
    else
        if(f==1)
            printf("%d\n",r+1);
        else
            printf("0\n");
}
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        for(i=0;i<n;i++)
            s[i]=MAXN;
        cha=0;
        while(k--)
        {
            input();
            gengxin();
        }
        print();
    }
    return 0;
}

结果还是一样的,在POJ和百练OJ上都是AC,但是还是不能通过九度的OJ。放弃之~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值