| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 719 人关注过本帖
标题:两道简单的程序题欢迎大家写出各种不同的解法.
只看楼主 加入收藏
_shuxian
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-8-12
结帖率:0
收藏
已结贴  问题点数:50 回复次数:8 
两道简单的程序题欢迎大家写出各种不同的解法.
 1、现有21根火柴,两人轮流取,每人每次可取走1- 4根,不可多取,也不能不取,谁取最后一根火柴则谁输。请编写一个程序进行人机对弈,要求人先取,计算机后取;计算机一方为“常胜将军”。要求程序运行效果如下图。
  " border="0" />
2、编程完成商人过河游戏:有三个商人带着三个随从和货物过河,船每次最多只能载两个人,由他们自己划行,并且如何乘船渡河的大权由商人掌握。要求保证在过河期间的任一岸上商人的人数要大于或等于随从的人数,否则随从会杀死商人抢走货物。设计一个符合上述要求的商人过河的游戏。要求程序运行效果如下图。

  (1)有游戏规则提示,按任意键开始游戏,按“Q”键退出游戏;

" border="0" />
(2)程序开始,分别提示每次从河岸这边到那边、从河岸那边到这边的商人和随从人数;
" border="0" />
" border="0" />
" border="0" />
(3)如果输入错误(如一次输入的人数不得超过2个、过河的人数不得超过2个),则提示错误并且按任意键重新开始游戏;
" border="0" />

(4)当任一河岸的商人数小于随从数时,提示商人被杀,按任意键继续游戏;
" border="0" />
" border="0" />

" border="0" />
(5)当游戏成功时提示游戏成功,按任意键结束游戏。
" border="0" />

 (注:程序可能用到的库函数及函数如下:

  #include <conio.h>

  void window(int left, int top, int right, int bottom); 定义活动文本模式窗口,例如:window(1,1,25,80);

  void textbackground(int color); 选择新的文本背景颜色(蓝颜色的值为LIGHTBLUE);

  void textcolor(int color); 在文本模式中选择新的字符颜色(黄颜色的值为YELLOW);

  void gotoxy(int x, int y); 在文本窗口中设置光标(x和y的值分别为行列坐标)。)
搜索更多相关主题的帖子: 解法 简单 
2009-08-12 20:31
CrystalFan
Rank: 8Rank: 8
来 自:江苏南京
等 级:蝙蝠侠
帖 子:187
专家分:802
注 册:2009-7-30
收藏
得分:10 
第一题很简单,每一轮里,计算机保证人和计算机所取火柴总数为5即可,到第四次完了,一定取了20根,最后一根一定是“人”拿的。
第二题应该是一个模拟,判断输入是否合法、是否出现商人被杀死和游戏是否成功即可。
2009-08-13 13:52
UserYuH
Rank: 12Rank: 12Rank: 12
来 自:毅华
等 级:火箭侠
威 望:8
帖 子:720
专家分:3300
注 册:2009-8-10
收藏
得分:10 
小东西都要动手统编编才好玩。
有些程序看是容易做得难,而有些是看得难做得不难。

ain()
{
 int n,x,y;
 n=21;
 printf("* turn. Each one each time takes 1 to 4 sticks. The *\n");
 printf("* one who takes the last stick will lose the game.  *\n");
 printf("*****************************************************\n");
 printf(" >> ----------------Game Begin ------------------ << \n");
 while(n)
   {
    loop:
    if(n>=4)
    y=4;
    else
    y=n;
    printf("How many stick do you wish to take(1~%d)?",y);
    scanf("%d",&x);
    if(x<1||x>4||x>n)goto loop;
    n=n-x;
    if(n!=0)
      {
       printf(" %d stick left in the pile.\n",n);
      }
    else
      {
       printf("You have taken the last stick.\n");
       break;
      }
    if(n>16)
      x=n-16;
    else
      if(n>11)
    x=n-11;
      else
    if(n>6)
      x=n-6;
    else
      x=n-1;
    n=n-x;
    printf("Compute take %d stick.\n",x);
    printf(" %d stick left in the pile.\n",n);
   }
printf("* * * You lose!\n");
printf(" >> ---------------- Game Over ------------------ << \n");
scanf("%c%c",&y);
 
}

努力—前进—变老—退休—入土
2009-08-13 14:41
rofor
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:68
专家分:165
注 册:2009-6-12
收藏
得分:10 
某人的作业不会做居然想到了这种方法骗代码。

I'm rofor.
for(;;;);  :-)
RoFoR [AT] YaHoO [DoT] CN
2009-08-14 08:42
CCVC果冻爽
Rank: 4
等 级:业余侠客
帖 子:116
专家分:209
注 册:2009-7-31
收藏
得分:10 
哈哈,支持LS
2009-08-14 09:41
_shuxian
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-8-12
收藏
得分:0 
回复 3楼 UserYuH

3楼的别说的那么难听什么骗代码的说,郁闷,只是想看看大家有没有什么别的好(算法)思路拿出来分享!
这些题目 网上搜下 C语言经典100题 就有例题了!
代码已经写在http://hi.baidu.com/_shuxian/blog/item/be8e7dedb4af233426979106.html
2009-08-17 22:49
_shuxian
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-8-12
收藏
得分:0 
1、现有21根火柴,两人轮流取,每人每次可取走1- 4根,不可多取,也不能不取,谁取最后一根火柴则谁输。请编写一个程序进行人机对弈,要求人先取,计算机后取;计算机一方为“常胜将军”。
#include<stdio.h>
void main()
{
   int a=21,i;
   printf("* turn. Each one each time takes 1 to 4 sticks. The *\n");  
   printf("* one who takes the last stick will lose the game.   *\n");  
   printf("*****************************************************\n");  
   printf(" >> ------------------ Game Begin ------------------\n");
   while(a>0)
   {
      do{
         printf("How many stick do you wish to take(1~%d)?",a>4?4:a);
         scanf("%d",&i);
      }while(i>4||i<1||i>a);      /*接收正在确的输入*/
      if(a-i>0) printf(" %d stick left in the pile.\n",a-i);
      if((a-i)<=0)
      {
         printf(" You have taken the last stick.\n");
         printf(" * * * You lose! \n");
         printf(" >> ------------------ Game Over! ------------------\n");
         break;
      }
      else
         printf(" Compute take %d stick.\n",5-i);    /*输出计算机取的子数*/
      a-=5;
      printf(" %d stick left in the pile.\n",a);
   }
   getch();
}
2009-08-17 23:07
_shuxian
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-8-12
收藏
得分:0 
2、编程完成商人过河游戏:有三个商人带着三个随从和货物过河,船每次最多只能载两个人,由他们自己划行,并且如何乘船渡河的大权由商人掌握。要求保证在过河期间的任一岸上商人的人数要大于或等于随从的人数,否则随从会杀死商人抢走货物。设计一个符合上述要求的商人过河的游戏。
#include <stdio.h>
#include <conio.h>
#include <string.h>
/* English Version */
 
void dwframe()
{
window(1,1,25,80);
textbackground(LIGHTBLUE);
textcolor(YELLOW);
clrscr();
gotoxy(2,2);
printf("***************************************************************************");
/*printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓");*/
gotoxy(2,3);
printf("*"); /*printf("┃");*/
gotoxy(76,3);
printf("*"); /*printf("┃");*/
gotoxy(2,4);
printf("*"); /*printf("┃");*/
gotoxy(76,4);
printf("*"); /*printf("┃");*/
gotoxy(2,5);
printf("*"); /*printf("┃");*/
gotoxy(76,5);
printf("*"); /*printf("┃");*/
gotoxy(2,6);
printf("*"); /*printf("┃");*/
gotoxy(76,6);
printf("*"); /*printf("┃");*/
gotoxy(2,7);
printf("*"); /*printf("┃");*/
gotoxy(76,7);
printf("*"); /*printf("┃");*/
gotoxy(2,8);
printf("*"); /*printf("┃");*/
gotoxy(76,8);
printf("*"); /*printf("┃");*/
gotoxy(2,9);
printf("*"); /*printf("┃");*/
gotoxy(76,9);
printf("*"); /*printf("┃");*/
gotoxy(2,10);
printf("*"); /*printf("┃");*/
gotoxy(76,10);
printf("*"); /*printf("┃");*/
gotoxy(2,11);
printf("*"); /*printf("┃");*/
gotoxy(76,11);
printf("*"); /*printf("┃");*/
gotoxy(2,11);
printf("*"); /*printf("┃");*/
gotoxy(76,11);
printf("*"); /*printf("┃");*/
gotoxy(2,12);
printf("*"); /*printf("┃");*/
gotoxy(76,12);
printf("*"); /*printf("┃");*/
gotoxy(2,14);
printf("*"); /*printf("┃");*/
gotoxy(76,14);
printf("*"); /*printf("┃");*/
gotoxy(2,15);
printf("*"); /*printf("┃");*/
gotoxy(76,15);
printf("*"); /*printf("┃");*/
gotoxy(2,16);
printf("*"); /*printf("┃");*/
gotoxy(76,16);
printf("*"); /*printf("┃");*/
gotoxy(2,17);
printf("*"); /*printf("┃");*/
gotoxy(76,17);
printf("*"); /*printf("┃");*/
gotoxy(2,18);
printf("*"); /*printf("┃");*/
gotoxy(76,18);
printf("*"); /*printf("┃");*/
gotoxy(2,19);
printf("*"); /*printf("┃");*/
gotoxy(76,19);
printf("*"); /*printf("┃");*/
gotoxy(2,20);
printf("*"); /*printf("┃");*/
gotoxy(76,20);
printf("*"); /*printf("┃");*/
gotoxy(2,21);
printf("*"); /*printf("┃");*/
gotoxy(76,21);
printf("*"); /*printf("┃");*/
gotoxy(2,22);
printf("*"); /*printf("┃");*/
gotoxy(76,22);
printf("*"); /*printf("┃");*/
gotoxy(2,23);
printf("*"); /*printf("┃");*/
gotoxy(76,23);
printf("*"); /*printf("┃");*/
gotoxy(2,24);
printf("***************************************************************************");
/*printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛");*/
}
void helpf()
{
dwframe();
gotoxy(25,4);
printf("^_^ Welcome to the game! ^_^");
/*printf("^_^欢迎使用商人过河游戏!^_^");*/
gotoxy(4,6);
printf("Game Rules:");
/*printf("游戏说明:");*/
gotoxy(5,7);
printf("3 men take 3 retainers and goods to pass the river, the boat carrys");
/*printf("三个商人带着三个随从和货物过河,船每次最多只能载两个人,");*/
gotoxy(5,8);
printf("2 persons each time. In passing the river, at any bank number of ");
/*printf("要求在过河期间,任一岸上商人的人数要大于或等于随从的人数,");*/
gotoxy(5,9);
printf("men must be more than the number of the retainers, or the retainers");
/*printf("否则随从会杀掉商人抢走货物。");*/
gotoxy(5,10);
printf("will kill the men and take the goods.");
gotoxy(5,11);
printf("Game operations: (1) Input the number of men and retainers in turn;");
/*printf("游戏操作:(1)依次输入过河的商人和随从人数;");*/
gotoxy(5,12);
printf("                 (2) Input error keys, the game will restart.");
/*printf("          (2)非法输入按键将重新开始。");*/
gotoxy(2,13);
printf("***************************************************************************");
/*printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫");*/
}
char a[6];
char b[6];
void printcase(char a[],char b[])
{
int i,j,xa,xb,x0,ya,yb,y0;
   xa=xb=x0=ya=yb=y0=0;
   gotoxy(11,14);
printf("This bank                 That bank\n");
   /*printf("此岸                彼岸\n");*/
for(i=0;i<6;i++)
{
    if(a[i]=='A')
        xa++;
    else if (a[i]=='B')
        xb++;
    else if(a[i]=='0')
        x0++;
}
gotoxy(11,15);
for(i=1;i<=xa;i++)
         printf("Man ");
   /*printf("商人 ");*/
   gotoxy(11,16);
for(i=1;i<=xb;i++)
   printf("Retainer ");
   /*printf("随从 ");*/
   gotoxy(11,17);
   for(i=1;i<=x0;i++)
   printf("   ");
 
   for(j=0;j<6;j++)
   {
   if(b[j]=='A')
       ya++;
   else if(b[j]=='B')
       yb++;
else if(b[j]=='0')
       y0++;
   }
gotoxy(39,15);
for(j=1;j<=ya;j++)
   printf("Man ");
gotoxy(39,16);
for(j=1;j<=yb;j++)
   printf("Retainer ");
gotoxy(39,17);
for(j=1;j<=y0;j++)
   printf("        ");
if(xa==0 && xb==0 && ya==3 && yb==3)
{
   gotoxy(15,21);
   printf("Congratulations! Mission Completed!Any key to quit...");
   /*printf("恭喜你!你已成功帮助商人安全过河!按任意键退出...");*/
   getch();
   window(1,1,25,80);
   textbackground(BLACK);
   textcolor(LIGHTGRAY);
   clrscr();
   exit(0);
}
}
 
void main()
{
int i,x,y,key,ca,cb,j,aA,aB,bA,bB;
char ch;
Start:  
helpf();
gotoxy(15,16);
printf("Press any key to start game. (Q key to quit)...");
/*printf("请按任意键开始游戏(按Q键退出)...");*/
ch=getch();
if(ch=='Q'||ch=='q')
{
   window(1,1,25,80);
   textbackground(BLACK);
   textcolor(LIGHTGRAY);
   clrscr();
   exit(0);
}
helpf();
 
for(i=0;i<3;i++)
   a[i]='A';
for(i=3;i<6;i++)
   a[i]='B';
for(i=0;i<6;i++)
   b[i]='0';
printcase(a,b);
while(1)
{
   gotoxy(20,18);
   printf("Input the number of men to that bank: ");
   /*printf("输入去彼岸商人的人数: ");*/
   scanf("%d",&x);
   gotoxy(20,18);
   printf("Input the number of retainers to that bank: ");
   /*printf("输入去彼岸随从的人数: ");*/
   scanf("%d",&y);
   for(ca=0,cb=0,i=0;i<6;i++)  
   {
    if(a[i]=='A')
         ca++;
    else if(a[i]=='B')
         cb++;
   }
   if(x<0 || x>ca || y<0 || y>cb || x+y<1 ||x+y>2)  
   {
    gotoxy(15,21);
    printf("Input error! Press any key to restart...");
    /*printf("输入有误!!按任意键重新开始...");*/
    getch();
    goto Start;
   }
   for(i=1;i<=x;i++)  
   {
      for(j=0;j<6;j++)
        if(a[j]=='A')  
        {
          a[j]='0';
          break;
        }
   }
   for(i=1;i<=x;i++)  
   {
      for(j=0;j<6;j++)
        if(b[j]=='0')  
        {
          b[j]='A';
             break;
        }
   }
   for(i=1;i<=y;i++)  
   {
      for(j=0;j<6;j++)
        if(a[j]=='B')
        {
          a[j]='0';
          break;
        }
   }
   for(i=1;i<=y;i++)  
   {
      for(j=0;j<6;j++)
        if(b[j]=='0')  
        {
          b[j]='B';
          break;
     }
   }
   for(aA=0,aB=0,i=0;i<6;i++)  
   {
      if(a[i]=='A')
        aA++;
      else if(a[i]=='B')
        aB++;
   }
   for(bA=0,bB=0,i=0;i<6;i++)  
   {
      if(b[i]=='A')
        bA++;
      else if(b[i]=='B')
        bB++;
   }
 
   if((aA==3) ||(bA==3) || (aA==aB) || (bA==bB))  
   {
          helpf();
          printcase(a,b);
          }
   else
   {
    gotoxy(15,21);
    printf("The man was killed. Press any key to restart...");
    /*printf("商人被杀,按任意键重新开始...");*/
    getch();
    goto Start;
   }
   gotoxy(20,18);
   printf("Input the number of men to this bank: ");
   /*printf("输入回此岸商人的人数: ");*/
   scanf("%d",&x);
   gotoxy(20,18);
   printf("Input the number of retainers to this bank: ");
   /*printf("输入回此岸随从的人数: ");*/
   scanf("%d",&y);
   for(ca=0,cb=0,i=0;i<6;i++)
   {
      if(b[i]=='A')
        ca++;
      else if(b[i]=='B')
        cb++;
   }
   if(x<0 || x>ca || y<0 || y>cb || x+y<1 || x+y>2)  
   {
    gotoxy(15,21);
    printf("Input error! Press any key to restart...");
       /*printf("输入有误!!按任意键重新开始...");*/
       getch();
      goto Start;
   }
   for(i=1;i<=x;i++)  
   {
      for(j=0;j<6;j++)
        if(b[j]=='A')  
        {
          b[j]='0';
          break;
        }
   }
   for(i=1;i<=x;i++)  
   {
      for(j=0;j<6;j++)
        if(a[j]=='0')  
        {
          a[j]='A';
          break;
     }
   }
   for(i=1;i<=y;i++)  
   {
      for(j=0;j<6;j++)
       if(b[j]=='B')  
       {
         b[j]='0';
         break;
       }
   }
   for(i=1;i<=y;i++)  
   {
      for(j=0;j<6;j++)
       if(a[j]=='0')
       {
         a[j]='B';
     break;
       }
   }
   for(aA=0,aB=0,i=0;i<6;i++)
   {
      if(a[i]=='A')
        aA++;
      else if(a[i]=='B')
        aB++;
   }
   for(bA=0,bB=0,i=0;i<6;i++)  
   {
      if(b[i]=='A')
        bA++;
      else if(b[i]=='B')
        bB++;
   }
if((aA==3) ||(bA==3) || (aA==aB) || (bA==bB))
{
      helpf();
      printcase(a,b);
   }
   else  
   {
       gotoxy(15,21);
       printf("The man was killed. Press any key to restart...");
       /*printf("商人被杀,按任意键重新开始...");*/
         getch();
         goto Start;
   }
}
}
 
(大家还有没有什么别的想法,继续往下写!)
2009-08-17 23:08
快速回复:两道简单的程序题欢迎大家写出各种不同的解法.
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023632 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved