欧几里得算法——欧几里得游戏

题目:一开始,板上写有两个不相等的正整数.两个玩家交替写数字,每一次,当前玩家都必须在板上写出任意两个板上数字的差,而且这个数字必须是新的(且为正),也就是说,不能与板上任何一个已有的数字相同.当玩家再也写不出新数字时,他就输了.请问,你是选择先行动还是后行动呢?

 1 import java.util.Scanner;
 2 
 3 /**
 4  * Created by Administrator on 14-7-16.
 5  */
 6 public class EuclidGame {
 7     public static void main(String[] args){
 8         int number=0;
 9         System.out.println("please give the number:");
10         Scanner scanner=new Scanner(System.in);
11         String str=scanner.nextLine();
12         int a=Integer.parseInt(str);
13         str=scanner.nextLine();
14         int b=Integer.parseInt(str);
15         winGame(gcd(a>b?a:b,a<=b?a:b),a>b?a:b);
16     }
17     public static int gcd(int a,int b){
18         int r;
19         while(b!=0){
20             r=a;
21             a=b;
22             b=r%a;
23         }
24         return a;
25     }
26     public static void winGame(int number,int max){
27         if((max/number)%2!=0){
28             System.out.println("the first one wins!");
29         }
30         else {
31             System.out.println("the second one wins");
32         }
33     }
34 }

转载于:https://www.cnblogs.com/mingcaoyouxin/p/3849559.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值