12个球的程序.....

  • 来源: 编程中国 作者: 若水   2008-04-22/16:34
  •       微软的一道题目...上班很无聊 写来玩玩
    有12个外表一模一样的小球,其中有一个的质量和其他11个球不一样(11个球的质量完全相同),而且该球不知道是轻点还是重点。现在给你一个天平(无砝码),只有3次测量机会,请你找出该球。球号从a到l(小写)

    #include <stdio.h>
    #include<time.h>
    #include<stdlib.h>

    #define LETTER_HEAD 97      //a的起始
    #define BALL_NUM 12         //球个数
    #define NORMAL_WEIGHT 2     //一般球的重量
    #define HIGH_WEIGHT 3       //球超重
    #define LOW_WEIGHT 1        //球失重...
    #define LOOP 3              //循环次数(可称几次)
    #define FALSE -1        
    #define TRUE 0

    int badBallWeight;
    char badBall;


    //得到坏球的字符
    char getBadBall()
    {
        srand((unsigned)time(NULL));
        return (char)(random()%BALL_NUM+LETTER_HEAD);
    }

    //得到坏球的重量
    void getBadBallWeight()
    {
        int type;
        srand((unsigned)time(NULL));
        type = random()%2;
       
        if(1==type)
        {   
            badBallWeight = HIGH_WEIGHT;
        }
        else
        {
            badBallWeight = LOW_WEIGHT;
        }
    }


    //开启了debug后 返回FALSE
    int debug(char* str)
    {
        if(strcmp(str, "exit") == 0)
        {
            exit(0);
        }

        if(strcmp(str, "cheat") == 0)
        {
            printf("CHEAT : bad ball is %c, weight is %d\n", badBall, badBallWeight);
            return FALSE;
        }

        return TRUE;
    }

    //验证球的名字,不在a-l之间的返回FALSE
    int verifyBallsName(char* str)
    {
        char* p;
       
        for(p = str; *p != '\0'; p++)
        {
            if(*p<'a' || *p>'l')
            {
                return FALSE;
            }
        }
       
        return TRUE;
    }


    //验证一侧的球有无重复,重复返回FALSE
    int verifySingleRepeat(char* str)
    {
        char* p, *temp;
        int repeat;
       
        for(p = str; *p != '\0'; p++)
        {
            repeat = 0;
            for(temp = str; *temp != '\0'; temp++)
            {
                if(*p == *temp)
                    repeat++;   
                
            }
            
            if(repeat > 1)
                return FALSE;
        }
       
        return TRUE;
    }

    //验证一侧的错误性 有错打印并返回FALSE,无错返回有几个球
    int verifySingle(char* str)
    {
        int err;

        err = debug(str);
        if(FALSE == err)
            return FALSE;
       
        err = verifyBallsName(str);
        if(FALSE == err)
        {
            printf("\nERROR: balls name error (Usage:abcdefghijkl)\n");
            return FALSE;
        }
       
        err = verifySingleRepeat(str);
        if(FALSE == err)
        {
            printf("\nERROR: balls name repeat\n");
            return FALSE;
            
        }
       
        return TRUE;
    }
    #p#分页标题#e#
    //得到单侧的重量
    int getSingleWeight(char* str)
    {
        int ballsNum, haveBadBall;
        char* p;
       
        ballsNum = strlen(str);
        haveBadBall = 0;
       
        for(p = str; *p != '\0'; p++)
        {
            if(*p == badBall)   
                haveBadBall++;
        }
       

        return ((ballsNum-haveBadBall)*NORMAL_WEIGHT+haveBadBall*badBallWeight);

    }

    int verifyMultiBallName(char* leftStr, char* rightStr)
    {
        char *l, *r;
        
        for(l = leftStr; *l != '\0'; l++)
        {
            for(r = rightStr; *r != '\0'; r++)
            {
                if(*l == *r)
                    return FALSE;
            }
        }
        
        return TRUE;
    }


    int main()
    {
        int loopTime, err;
        int leftWeight, rightWeight;
        char left[128],right[128];
        char bad[16];
       
        printf("有12个外表一模一样的小球,其中有一个的质量和其他11个球不一样(11个球的质量完全相同),\
        而且该球不知道是轻点还是重点。现在给你一个天平(无砝码),只有3次测量机会,请你找出该球。球号从a到l(小写)\n");
        badBall = getBadBall();
        getBadBallWeight();
            
        for(loopTime = 1; loopTime <= LOOP; loopTime++)
        {
            printf("pls input left balls:   ");
            scanf("%s", left);   
            
            err = verifySingle(left);
            if(FALSE == err)
                goto error;
                   
            leftWeight = getSingleWeight(left);
            
            printf("pls input right balls:   ");
            scanf("%s", right);

            err = verifySingle(right);
            if(FALSE == err)
                goto error;
            
            rightWeight = getSingleWeight(right);
            
            err = verifyMultiBallName(left, right);
            if(FALSE == err)
            {
                printf("\nERROR: one ball both in left and right\n");
                goto error;
            }
            
            if(leftWeight > rightWeight)
            {
                printf("result %d : left > right\n", loopTime);
            }
            else if(leftWeight < rightWeight)
            {
                printf("result %d : left < right\n", loopTime);
            }
            else
            {
                printf("result %d : left = right\n", loopTime);
            }
            
            continue;
    error:
            loopTime--;
        }
        
        printf("pls input the bad ball name:");
        
        scanf("%s", bad);
        if(*bad == badBall)
        {
            printf("\ncongratulation,your selected is right!\n");
        }
        else
        {
            printf("\nsorry,your selected is wrong!\n");
        }
        
        return 0;
    }
    #p#分页标题#e#

     


    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多