hdu,poj博弈简单题

本文介绍了几个在HDU和POJ上的博弈类编程题目,包括A Multiplication Game、Brave Game、Good Luck in CET-4 Everybody! 和 kiki's game。通过给出的题目链接和描述,讨论了不同游戏的策略和解决方案,涉及到玩家如何通过特定的决策来确保胜利的策略。同时,文章提到了模仿思想在解决此类问题中的应用。
摘要由CSDN通过智能技术生成

hdu1517

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1517

A Multiplication Game

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2846    Accepted Submission(s): 1630


Problem Description

 

Stan and Ollie play the game of multiplication by multiplying an integer p by one of the numbers 2 to 9. Stan always starts with p = 1, does his multiplication, then Ollie multiplies the number, then Stan and so on. Before a game starts, they draw an integer 1 < n < 4294967295 and the winner is who first reaches p >= n.
 


 

Input

 

Each line of input contains one integer number n.
 


 

Output

 

For each line of input output one line either  

Stan wins.  

or  

Ollie wins.

assuming that both of them play perfectly.
 


 

Sample Input

 

  
  
162 17 34012226
 


 

Sample Output

 

  
  
Stan wins. Ollie wins. Stan wins.
 

 

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    __int64 i,m,n[2]={9,2},k;
    while(scanf("%I64d",&m)>0) 
    {
		i=1;k=0;
		while(i<m)
		{
			i=i*n[k];
			k=(k+1)%2;
		}
		if(k==1) printf("Stan wins.\n");
		else printf("Ollie wins.\n"); 
    }
    return 0;
}


hdu1846

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1846

Brave Game

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4571    Accepted Submission(s): 3001


Problem Description
十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫《勇敢者的游戏》(英文名称:Zathura),一直到现在,我依然对于电影中的部分电脑特技印象深刻。
今天,大家选择上机考试,就是一种勇敢(brave)的选择;这个短学期,我们讲的是博弈(game)专题;所以,大家现在玩的也是“勇敢者的游戏”,这也是我命名这个题目的原因。
当然,除了“勇敢”,我还希望看到“诚信”,无论考试成绩如何,希望看到的都是一个真实的结果,我也相信大家一定能做到的~

各位勇敢者要玩的第一个游戏是什么呢?很简单,它是这样定义的:
1、  本游戏是一个二人游戏;
2、  有一堆石子一共有n个;
3、  两人轮流进行;
4、  每走一步可以取走1…m个石子;
5、  最先取光石子的一方为胜;

如果游戏的双方使用的都是最优策略,请输出哪个人能赢。
 

Input
输入数据首先包含一个正整数C(C<=100),表示有C组测试数据。
每组测试数据占一行,包含两个整数n和m(1<=n,m<=1000),n和m的含义见题目描述。
 

Output
如果先走的人能赢,请输出“first”,否则请输出“second”,每个实例的输出占一行。
 

Sample Input
  
  
2 23 2 4 3
 

Sample Output
  
  
first second
 

水方法:对于一个总数为m的石子堆,一次至多去除n个,那么当石子数目小于等于n时,必定为第一个人胜利。而当石子有n+1个时,必定为第二个人胜利,因为无论第一个人取多少个石子,必定小于n+1,而二个人一共取得石子总数一定可以组成n+1。由此推知,对于n+2,我们第一次只取一个石子,那么轮到第二个人取石子时石子堆一共有n+1个石子,又变成了刚才那种情况,所以自n+2到n+n+1都必为第一个人胜利。往下推就可得出公式。

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    int t,m,n,a[1010];
    scanf("%d",&t);
    while(t--)
    {
              scanf("%d%d",&m,&n);
              if(m%(n+1)==0) printf("second\n");
              else printf("first\n");
    }
    return 0;
}


 hdu1847

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1847

Good Luck in CET-4 Everybody!

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3659    Accepted Submission(s): 2306


Problem Description

 

大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知道的Kiki和Cici都是如此。当然,作为在考场浸润了十几载的当代大学生,Kiki和Cici更懂得考前的放松,所谓“张弛有道”就是这个意思。这不,Kiki和Cici在每天晚上休息之前都要玩一会儿扑克牌以放松神经。
“升级”?“双扣”?“红五”?还是“斗地主”?
当然都不是!那多俗啊~
作为计算机学院的学生,Kiki和Cici打牌的时候可没忘记专业,她们打牌的规则是这样的:
1、  总共n张牌;
2、  双方轮流抓牌;
3、  每人每次抓牌的个数只能是2的幂次(即:1,2,4,8,16…)
4、  抓完牌,胜负结果也出来了:最后抓完牌的人为胜者;
假设Kiki和Cici都是足够聪明(其实不用假设,哪有不聪明的学生~),并且每次都是Kiki先抓牌,请问谁能赢呢?
当然,打牌无论谁赢都问题不大,重要的是马上到来的CET-4能有好的状态。

Good luck in CET-4 everybody!
 


 

Input

 

输入数据包含多个测试用例,每个测试用例占一行,包含一个整数n(1<=n<=1000)。
 


 

Output

 

如果Kiki能赢的话,请输出“Kiki”,否则请输出“Cici”,每个实例的输出占一行。
 


 

Sample Input

 

  
  
1 3
 


 

Sample Output

 

  
  
Kiki Cici
 


 

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    int i,m,n,a[1010];
    a[0]=2;a[1]=1;a[2]=1;
	for(i=3;i<=1000;i++)
	{
		n=1;
		while(n<=i)
		{
			if(a[i-n]==2)
			{
				a[i]=1;break;
			}
			a[i]=2;
			n*=2;
		}
	}
    while(scanf("%d",&m)>0) 
    {
		if(a[m]==1) printf("Kiki\n");
		else printf("Cici\n"); 
    }
    return 0;
}


hdu2147

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2147

kiki's game

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/1000 K (Java/Others)
Total Submission(s): 5153    Accepted Submission(s): 3028


Problem Description
Recently kiki has nothing to do. While she is bored, an idea appears in his mind, she just playes the checkerboard game.The size of the chesserboard is n*m.First of all, a coin is placed in the top right corner(1,m). Each time one people can move the coin into the left, the underneath or the left-underneath blank space.The person who can't make a move will lose the game. kiki plays it with ZZ.The game always starts with kiki. If both play perfectly, who will win the game?
 

Input
Input contains multiple test cases. Each line contains two integer n, m (0<n,m<=2000). The input is terminated when n=0 and m=0.

 

Output
If kiki wins the game printf "Wonderful!", else "What a pity!".
 

Sample Input
  
  
5 3 5 4 6 6 0 0
 

Sample Output
  
  
What a pity! Wonderful! Wonderful!
 

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    int m,n;
    while(scanf("%d%d",&m,&n)>0,m,n) 
    {
		if(m*n%2==0) printf("Wonderful!\n");
		else printf("What a pity!\n"); 
    }
    return 0;
}


不知代码对不对,反正AC了

 

POJ 2484

地址:http://poj.org/problem?id=2484

A Funny Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3475 Accepted: 2047

Description

Alice and Bob decide to play a funny game. At the beginning of the game they pick n(1 <= n <= 10 6) coins in a circle, as Figure 1 shows. A move consists in removing one or two adjacent coins, leaving all other coins untouched. At least one coin must be removed. Players alternate moves with Alice starting. The player that removes the last coin wins. (The last player to move wins. If you can't move, you lose.)

Figure 1

Note: For n > 3, we use c1, c2, ..., cn to denote the coins clockwise and if Alice remove c2, then c1 and c3 are NOT adjacent! (Because there is an empty place between c1 and c3.)

Suppose that both Alice and Bob do their best in the game.
You are to write a program to determine who will finally win the game.

Input

There are several test cases. Each test case has only one line, which contains a positive integer n (1 <= n <= 10 6). There are no blank lines between cases. A line with a single 0 terminates the input.

Output

For each test case, if Alice win the game,output "Alice", otherwise output "Bob".

Sample Input

1
2
3
0

Sample Output

Alice
Alice
Bob

 

利用了一个模仿的思想。当硬币数小于等于2时,就是Alice赢。当硬币数在3-4之间,就是Bob赢。当硬币大于4时,Bob就对称模仿Alice,例如8个硬币按1-8标号,Alice第一次拿走了编号为1的硬币,Bob拿走对称的硬币5,若Alice拿走1,2两枚硬币,Bob就拿5,6两枚,那么最后一定是Bob胜利。

#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
	int m;
	while(scanf("%d",&m)>0,m)
	{
		if(m==0) break;
		if(m<=2) puts("Alice");
		else puts("Bob");
	}
	return 0;
}


 poj2348

地址:http://poj.org/problem?id=2348

Euclid's Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6827 Accepted: 2781

Description

Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the first player, subtracts any positive multiple of the lesser of the two numbers from the greater of the two numbers, provided that the resulting number must be nonnegative. Then Ollie, the second player, does the same with the two resulting numbers, then Stan, etc., alternately, until one player is able to subtract a multiple of the lesser number from the greater to reach 0, and thereby wins. For example, the players may start with (25,7):
         25 7

         11 7

          4 7

          4 3

          1 3

          1 0

an Stan wins.

Input

The input consists of a number of lines. Each line contains two positive integers giving the starting two numbers of the game. Stan always starts.

Output

For each line of input, output one line saying either Stan wins or Ollie wins assuming that both of them play perfectly. The last line of input contains two zeroes and should not be processed.

Sample Input

34 12
15 24
0 0

Sample Output

Stan wins
Ollie wins

题意:每次可以拿大的数减去小的数的倍数,但不能减到小于零,最后先得到零的人胜利。

可以推断:每次减一小的数的一倍就可以的到下一组数据时就看操作数的奇偶性,否则就看到要减多倍的那组数据的操作数的奇偶性。

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
int main()
{
	int m,n,i;
	while(scanf("%d%d",&m,&n)>0,m,n)
	{
		i=0;
		while(m!=0)
		{
			if(m<n)
			{
				n^=m;
				m^=n;
				n^=m;
			}
			i++;
			if((m/n)>1&&m%n!=0) break;
			m=m%n;
		}
		if(i%2==1) puts("Stan wins");
		else puts("Ollie wins");
	}
	return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值