level 1
import java.util.HashMap;
import java.util.Map;
public class WhoWin
{
static Map<String, String> map=new HashMap<String, String>();
public static int who(String word)
{
if (word.length() < 3)
{
return 1;
}
else if (word.length() == 3)
{
if(!map.containsKey(word)){
for (int i = 0; i < 2; i++)
{
if (word.charAt(i) < word.charAt(i + 1))
{
map.put(word, "1");
return 1;
}
}
}else{
return Integer.parseInt(map.get(word));
}
}
else if (word.length() > 3)
{
if(!map.containsKey(word)){
for (int i = 0; i < word.length(); i++)
{
StringBuffer sb = new StringBuffer(word);
sb.deleteCharAt(i);
if(!map.containsKey(sb.toString())){
if (isIncrease(sb.toString()))
{
map.put(word, "1");
return 1;
}
else if (who(sb.toString()) == 0)
{
map.put(word, "1");
return 1;
}
}else{
if(map.get(sb.toString()).equals("0")){
return 1;
}
}
}
}else{
return Integer.parseInt(map.get(word));
}
}
map.put(word, "0");
return 0;
}
public static boolean isIncrease(String str)
{
for (int i = 0; i < str.length() - 1; i++)
{
if (str.charAt(i) >= str.charAt(i + 1))
{
return false;
}
}
return true;
}
public static void main(String[] args)
{
String str = "hdeihfuiehwuif";
if (who(str) == 1)
{
System.out.println("甲会赢!");
}
else if (who(str) == 0)
{
System.out.println("乙会赢!");
}
}
}
有不同方法的同学给我留言,虽然代码还有很多可以优化的地方
2013年12月07日 19点12分