日期:2014-05-18 浏览次数:20679 次
public class Test {
public static void main(String[] args) {
String string="vgwgpkxxkqmnx";
System.out.println(who(string));
}
public static int who(String word){
String word2=word;
int maxIndex;//逆序最大数
int count=0;//count为奇数甲操作,count为偶数乙操作
while(maxCount(word2)>0){
//System.out.println(word2);
maxIndex=maxCount(word2);
word2=word2.substring(0,maxIndex)+word2.substring(maxIndex+1,word2.length());
count++;
}
return count%2;
}
private static int maxCount(String in){
int[] a=new int[in.length()];
int max=0;
int maxIndex=0;
char[] chs=in.toCharArray();
for(int i=0;i<in.length()-1;i++)
for(int j=i+1;j<in.length();j++){
if(chs[i]>=chs[j]){
a[i]++;
a[j]++;
}
}
for(int i=0;i<a.length;i++){
//System.out.print(a[i]+" ");
if(a[i]>max){
max=a[i];
maxIndex=i;
}
}
//System.out.println();
return maxIndex;
}
}