[原创]我的北大ACM POJ 1013解答

我的北大ACM POJ 1013解答
又一个水题,居然还贡献了3次WA。。。

Source Code

Problem: 1013
User: absolute
Memory: 204K
Time: 0MS
Language: C++
Result: Accepted
  • Source Code
  • #include <stdio.h>
    #include <string>
    void POJ1013();
    int main()
    {
    POJ1013();
    return 0;
    }
    void POJ1013()
    {
    int i,k,casen;
    scanf("%d",&casen);
    while(casen>0)
    {
    casen--;
    bool real[12]={0};
    int heavy[12]={0};
    int light[12]={0};
    for(k=0;k<3;k++)
    {
    char input[3][8]={0};
    int j;
    scanf("%s %s %s",&input[0],&input[1],&input[2]);
    //平衡则放入真数组,表示这次称的都是真币
    if(strcmp(input[2],"even")==0)
    {
    j=0;
    while(input[0][j]!='\0')
    {
    real[input[0][j]-'A']=true;
    real[input[1][j]-'A']=true;
    j++;
    }
    }
    //右边起来,左边怀疑为重,右边怀疑为轻
    else if(strcmp(input[2],"up")==0)
    {
    j=0;
    while(input[0][j]!='\0')
    {
    heavy[input[0][j]-'A']++;
    light[input[1][j]-'A']++;
    j++;
    }
    }
    //右边下降,左边怀疑为轻,右边为重
    else if(strcmp(input[2],"down")==0)
    {
    j=0;
    while(input[0][j]!='\0')
    {
    light[input[0][j]-'A']++;
    heavy[input[1][j]-'A']++;
    j++;
    }

    }
    }
    char maxnum='A';
    int heavyorlight=0;
    //假币必然是不在真数组中,而且它必然只能在重数组和轻数组之一出现,而不能同时出现
    //但是有可能被怀疑的只称过一次,所以必须选出被怀疑最多次的那个
    for(i='A';i<'M';i++)
    {
    if(real[i-'A'])
    continue;
    int temp=0;
    if(light[i-'A']>0&&heavy[i-'A']==0)
    {
    if(heavyorlight==1)
    temp=light[maxnum-'A'];
    else if(heavyorlight==2)
    temp=heavy[maxnum-'A'];
    if(light[i-'A']>temp)
    {
    heavyorlight=1;
    maxnum=i;
    }
    continue;
    }
    if(light[i-'A']==0&&heavy[i-'A']>0)
    {
    if(heavyorlight==1)
    temp=light[maxnum-'A'];
    else if(heavyorlight==2)
    temp=heavy[maxnum-'A'];
    if(heavy[i-'A']>temp)
    {
    heavyorlight=2;
    maxnum=i;
    }
    continue;
    }
    }
    if(heavyorlight==1)
    printf("%c is the counterfeit coin and it is light.\n",maxnum);
    else
    printf("%c is the counterfeit coin and it is heavy.\n",maxnum);
    }
    }

posted on 2009-04-12 21:51  absolute  阅读(583)  评论(0编辑  收藏  举报

导航