67,513
社区成员
发帖
与我相关
我的任务
分享
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Test {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("请输入字母");
int y = who(s.next());
System.out.println(y);
}
public static int who(String word){
StringBuilder s1 = new StringBuilder(word);
StringBuilder s2;
int x = 0;
while (true) {
x++;
int i =getch(s1.toString());
s1.deleteCharAt(i);
s2=new StringBuilder(s1.toString());
char[] ch = s2.toString().toCharArray();
Arrays.sort(ch);
boolean b = Arrays.equals(ch, s1.toString().toCharArray());
if (b) {
if (x%2!=0) {
return 1;
}else{
return 0;
}
}else{
continue;
}
}
}
public static int getch(String s){
char[] ch = s.toCharArray();
int num = 0;
int num1 = 0;
int num2 = 0;
Character c=null ;
boolean b = false;
Random r = new Random();
for (int i = 0; i < ch.length-1; i++) {
num2 ++;
for (int j = i+1; j < ch.length; j++) {
if (ch[i]==ch[j]) {
b = true;
num+=1;
}
}
if (num!=0) {
if (num1!=0) {
if (num1<num) {
num1 =num;
c= ch[i];
}
}else{
num1 = num;
c = ch[i];
}
}
}
System.out.println(c);
if (!b&&num2+1 ==ch.length) {
System.out.println("没有重复 的");
return r.nextInt(ch.length);
}else{
return s.indexOf(c.toString());
}
}
}
我用的比较简单,如果真的够聪明的话,如果剩下ZAF 那么甲是不是就会选择把Z拿掉?这样就赢了?
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;
}
}