• 当前位置:首页>>C语言>>C语言编程实例>>C趣味程序百例(29)
  • C趣味程序百例(29)
  • 89.抢 30
       这是中国民间的一个游戏。两人从1开始轮流报数,每人每次可报一个数或两个连续的数,谁先报到30,谁就为胜方。
    *问题分析与算法设计
       本题与上题类似,算法也类似,所不同的是,本谁先走第一步是可选的。若计算机走第一步,那么计算机一定是赢家。若人先走一步,那么计算机只好等待人犯错误,如果人先走第一步且不犯错误,那么人就会取胜;否则计算机会抓住人的一次错误使自己成为胜利者。
    *程序与程序注释
    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    int input(int t);
    int copu(int s);
    void main()
    {
       int tol=0;
       printf("\n* * * * * * * *catch thirty* * * * * * * \n");
       printf("Game Begin\n");
       randomize();                /*初始化随机数发生器*/
       if(random(2)==1)      /*取随机数决定机器和人谁先走第一步*/
          tol=input(tol);    /*若为1,则余元走第一步*/
       while(tol!=30)        /*游戏结束条件*/
          if((tol=copu(tol))==30)       /*计算机取一个数,若为30则机器胜利*/
             printf("I lose! \n");
          else
             if((tol=input(tol))==30)     /*人取一个数,若为30则人胜利*/
                printf("I lose! \n");
       printf(" * * * * * * *  *Game Over * * * * * * * *\n");
    }
    int input(int t)
    {
       int a;
       do{
          printf("Please count:");
          scanf("%d",&a);
          if(a>2||a<1||t+a>30)
             printf("Error input,again!");
          else
             printf("You count:%d\n",t+a);
       }while(a>2||a<1||t+a>30);
       return t+a;        /*返回当前的已经取走的数累加和*/
    }
    int copu(int s)
    {
       int c;
       printf("Computer count:");
       if((s+1)%3==0)          /*若剩余的数的模为1,则取1*/
          printf(" %d\n",++s);
       else if((s+2)%3==0)
       {
          s+=2;     /*若剩余的数的模为2,则取2*/
          printf(" %d\n",s);
       }
       else
       {
          c=random(2)+1;     /*否则随机取1或2*/
          s+=c;
          printf(" %d\n",s);
       }
       return s;
    }
    *运行示例



    *思考题
       巧夺偶数。桌子上有25颗棋子,游戏双方轮流取子,每人每次最少取走一颗棋子,最多可取走3颗棋子。双方照这样取下去,直到取光所有的棋子。于是双方手中必然一方为偶数,一方为奇数,偶数方为胜者。请编程实现人机游戏。



    90.搬山游戏
       设有n座山,计算机与人为比赛的双方,轮流搬山。规定每次搬山的数止不能超 过k座,谁搬最后一座谁输。游戏开始时。计算机请人输入山的总数(n)和每次允许搬山的最大数止(k)。然后请人开始,等人输入了需要搬走的山的数目后,计算机马上打印出它搬多少座山,并提示尚余多少座山。双方轮流搬山直到最后一座山搬完为止。计算机会显示谁是赢家,并问人是否要继续比赛。若人不想玩了,计算机便会统计出共玩了几局,双方胜负如何。
    *问题分析与算法设计
       计算机参加游戏时应遵循下列原则:
       1) 当:
          剩余山数目-1<=可移动的最大数k 时计算机要移(剩余山数目-1)座,以便将最后一座山留给人。
       2)对于任意正整数x,y,一定有:
                0<=x%(y+1)<=y
       在有n座山的情况下,计算机为了将最后一座山留给人,而且又要控制每次搬山的数目不超过最大数k,它应搬山的数目要满足下列关系:
                (n-1)%(k+1)
       如果算出结果为0,即整除无余数,则规定只搬1座山,以防止冒进后发生问题。
       按照这样的规律,可编写出游戏程序如下:
    #include<stdio.h>
    void main()
    {
       int n,k,x,y,cc,pc,g;
       printf("More Mountain Game\n");
       printf("Game Begin\n");
       pc=cc=0;
       g=1;
       for(;;)
       {
          printf("No.%2d game \n",g++);
       printf("---------------------------------------\n");
       printf("How many mpuntains are there?");
       scanf("%d",&n);
       if(!n) break;
       printf("How many mountains are allowed to each time?");
       do{
          scanf("%d",&k);
          if(k>n||k<1) printf("Repeat again!\n");
       }while(k>n||k<1);
       do{
          printf("How many mountains do you wish movw away?");
          scanf("%d",&x);
          if(x<1||x>k||x>n)      /*判断搬山数是否符合要求*/
          {
             printf("IIIegal,again please!\n");
             continue;
          }
          n-=x;
          printf("There are %d mountains left now.\n",n);
          if(!n)
          {
             printf("...............I win. You are failure...............\n\n");cc++;
          }
          else
          {
             y=(n-1)%(k+1);      /*求出最佳搬山数*/
             if(!y) y=1;
             n-=y;
             printf("Copmputer move %d mountains away.\n",y);
             if(n) printf(" There are %d mountains left now.\n",n);
             else
             {
                printf("...............I am failure. You win..................\n\n");
                pc++;
             }
          }
       }while(n);

       }
       printf("Games in total have been played %d.\n",cc+pc);
       printf("You score is win %d,lose %d.\n",pc,cc);
       printf("My score is win %d,lose %d.\n",cc,pc);
    }

    *运行示例
             Move Mountain Game
       

    [1] [2] 下一页