/** * User: Free * Date: 14-5-11 * Time: 上午12:52 */ public class PairGameEasy { public String able(int a, int b, int c, int d) { String Y = "Able to generate"; String N = "Not able to generate"; while(a!=c || b!=d){ if(c<a || d<b){ return N; } if(c>d){ c = c-d; } else if(d>c){ d = d-c; }else{ return N; } } if(a==c && b==d ) return Y; else return N; } public static void main(String args[]){ PairGameEasy pairGameEasy = new PairGameEasy(); System.out.println(pairGameEasy.able(1,2,3,8)); System.out.println(pairGameEasy.able(1,2,2,1)); System.out.println(pairGameEasy.able(2,2,2,999)); } }
topcoder上分3种解决
(1)递归
public String able(int a, int b, int c, int d) { String Y = "Able to generate"; String N = "Not able to generate"; { if(a>c || b>d)return N; if(able(a+b,b,c,d).equals(N)) return able(a,a+b,c,d); else return Y; }
(2)DP
public String able(int a, int b, int c, int d) { String Y = "Able to generate"; String N = "Not able to generate"; int dp[][] =new int[2000][2000]; dp[a][b]=1; for(int i=a;i<=c;i++){ for(int j=b;j<=d;j++) { if(dp[i][j]==1){ dp[i+j][j]=1; dp[i][i+j]=1; } } } if(dp[c][d]==1){ return Y; } return N; }
这个dp方法思路好,虽然在本程序中,效率低,算法不太好,但思想不错
(3)我用的是递减思路,应该是算法复杂度最小的,采用了gcd思路来计算的。最前面贴出来的。