创建一个充分发挥作用的“猜数字”游戏。该游戏由两名玩家进行,具体如下:
if A > B、“较低”if A < B或“正确”if A = B。有效条目的最低规格:
按照这一顺序:
发布于 2011-02-22 11:32:27
N,H=99,L=0,c=0,w=1;main(){char s[9];puts("Play as player 1 or 2: ");scanf("%d",&N);if(N-1){getchar();do{N=rand()%(H-L)+L+1;printf("My guess: %d\n",N);gets(s);if(*s=='c'){w=2;break;}if(*s-'l')H=N-1;else L=N-1;c++;}while(c<5);}else{N=rand()%99+1;while(c<5){puts("Enter guess: ");scanf("%d",&H);if(H==N){puts("correct");break;}else puts(H>N?"higher":"lower");c++;}if(c==5)w=2;}printf("Winner %d",w);}以一种更易读的形式。
main()
{
int i,N,H=100,L=0,c=0,w=1;
char s[10];
puts("Play as player 1 or 2: ");
scanf("%d",&i);
if(i-1)
{
getchar();
do{
N=rand()%(H-L)+L+1;
printf("My guess: %d\n",N);
gets(s);
if(s[0]=='c')break;
else if(s[0]=='h')H=N-1;
else L=N-1;
c++;
}while (c<5);
if(c<5)w=2;
}
else
{
N=rand()%99+1;
while (c<5)
{
puts("Enter another guess: ");
scanf("%d",&H);
if(H==N){printf("correct\n");break;}
else if(H>N)printf("higher\n");
else printf("lower\n");
c++;
}
if(c==5)w=2;
}
printf("Winner %d",w);
}发布于 2011-03-09 12:52:17
C#:
字符计数:带空格: 575无空格: 464
static void Main()
{
Action<object> w = s => Console.WriteLine(s);
Func<object, byte> r = t => { w(t); var s = Console.ReadLine(); return Convert.ToByte(s); };
var p = r("Player (1/2):");
int N = 100, g, i = 0, c, d;
var q = new List<int>(Enumerable.Range(0, N));
Func<Guid> x = Guid.NewGuid;
c = p == 1 ? r("Number:") : q.OrderBy(j => x()).First();
m: i++;
g = p == 2 ? r("Guess:") : q.OrderBy(j => x()).First();
d = g < c ? -1 : (g > c ? 1 : 0);
w(d == -1 ? "Higher" : (d == 1 ? "Lower" : "correct"));
q = q.Where(n => d == -1 ? n > g : n < g).ToList();
if(c != g && i < 5) goto m;
r(g);
}编辑do而现在是"Goto“(颤抖)
发布于 2011-02-23 15:44:48
好的老平原C
#include <stdio.h>
#define x(s) puts(s)
main(){int c,i,l,h,g;srand(time(NULL));p:x("You want to guess (1) or should I (2)?");scanf("%d",&c);i=5;if(c==2){x("Think a number 1..100");h=100;l=1;goto t;}if(c==1){x("Guess a number 1..100");h=rand()%100+1;goto g;}return 0;t:if(!i--)goto u;printf("%d (1)higher (2)lower (3)correct",g=rand()%(h-l)+l);scanf("%d",&c);if(c==1)l=g;if(c==2)h=g;if(c==3)goto c;goto t;g:if (!i--)goto c;scanf("%d",&g);if(g>h)x("lower");if(g<h)x("higher");if(g==h){x("correct");goto u;}goto g;u:x("You win");goto p;c:x("I win");goto p;}https://codegolf.stackexchange.com/questions/1112
复制相似问题