菜鸟的代码:
import java.util.*; import java.util.regex.*; import java.text.*; import java.math.*; public class PrimalUnlicensedCreatures { public int maxWins(int initialLevel, int[] grezPower) { int i,count,pre,level; boolean[] tag = new boolean[grezPower.length]; pre=-1; count=0; level = initialLevel; i=0; while(true){ if(level>grezPower[i]&&!tag[i]){ level = level + grezPower[i]/2; count++; tag[i]=true; } i++; if(i==grezPower.length){ i=i%grezPower.length; if(pre!=count) pre=count; else break; } } return count; } } //Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
大神的代码:
import java.util.*; import java.util.regex.*; import java.text.*; import java.math.*; public class PrimalUnlicensedCreatures { public int maxWins(int initialLevel, int[] grezPower) { Arrays.sort(grezPower); int n = grezPower.length; int i = 0; for (i = 0; i < n; i++) { if (initialLevel > grezPower[i]) { initialLevel += grezPower[i] / 2; } else return i; } return i; } } // Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
分析:
算法:遍历数组打败对手
对比:
1.大神的一个排序,顶过了我一堆代码。
2.因为没有排序,我考虑地要复杂些。
总结:
大神的代码,看起来就是那么爽啊。学习了,以后可以考虑用排序,一个Arrays.sort的事情,呵呵。