首页 > 其他 > 详细

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

时间:2014-07-19 20:26:58      阅读:349      评论:0      收藏:0      [点我收藏+]

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

 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 }

欧几里得算法——欧几里得游戏,布布扣,bubuko.com

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

原文:http://www.cnblogs.com/mingcaoyouxin/p/3849559.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!