Two people face two piles of stones and make a game. They take turns to take stones. As game rules, there are two different methods of taking stones: One scheme is that you can take any number of stones in any one pile while the alternative
is to take the same amount of stones at the same time in two piles. In the end, the first person taking all the stones is winner.Now,giving the initial number of two stones, can you win this game if you are the first to take stones and both sides have taken
the best strategy?
2 1 8 4 4 7
0 1 0
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
BigDecimal TWO=new BigDecimal("2");
BigDecimal FIVE=new BigDecimal("5");
BigDecimal EPS=new BigDecimal("0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001");
BigDecimal l=new BigDecimal("0");
BigDecimal r=new BigDecimal("5");
BigDecimal m=new BigDecimal("2.5");
BigDecimal ONE= new BigDecimal("1");
while(l.subtract(r).compareTo(EPS)<0){
m = l.add(r).divide(TWO);
if(m.multiply(m).subtract(FIVE).abs().compareTo(EPS.abs())<0) break;
if(m.multiply(m).subtract(FIVE).compareTo(EPS)<0) l = m;
else r = m;
}
//System.out.println(r);
r=r.add(ONE).divide(TWO);
while(scan.hasNext())
{
String str1=scan.next();
String str2=scan.next();
BigDecimal x=new BigDecimal(str1);
BigDecimal y=new BigDecimal(str2);
if(x.compareTo(y)>0)
{
TWO=x;x=y;y=TWO;
}
BigDecimal z=y.subtract(x);
BigDecimal w=r.multiply(z);
w=w.setScale(0, BigDecimal.ROUND_DOWN);
if(w.equals(x))
System.out.println(0);
else
System.out.println(1);
}
}
}

本文介绍了一个基于两堆石头的游戏胜负判断算法。游戏中两名玩家轮流从两堆石头中取石子,可以一次从一堆中取任意数量或者同时从两堆取相同数量的石头,直至取完为止。文章提供了一种通过输入两堆石头的初始数量来判断先手是否能赢得比赛的方法。

406

被折叠的 条评论
为什么被折叠?



