这道题真是虐心。总的路线是先判断扑克的type,type小的胜出。如果type相同,则按type内的规则继续比较。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
public class Solution{
public static void main(String[] args) throws Exception{
/**/
String hand1;
String hand2;
InputStreamReader ir;
BufferedReader br;
ir = new InputStreamReader(System.in);
br = new BufferedReader(ir);
try
{
hand1 = br.readLine();
hand2 = br.readLine();
}
catch (IOException e) {
throw e;
}
HashMap<String, Integer> pokerValue = new HashMap<>();
for(int i=2;i<11;i++)
pokerValue.put(Integer.toString(i), i);
pokerValue.put("J", 11);
pokerValue.put("Q", 12);
pokerValue.put("K", 13);
pokerValue.put("A", 14);
int[] A1 = new int[4];
int[] A2 = new int[4];
String[] str = hand1.split(",");
for(int i=0;i<4;i++)
A1[i] = pokerValue.get(str[i]);
str = hand2.split(",");
for(int i=0;i<4;i++)
A2[i] = pokerValue.get(str[i]);
Arrays.sort(A1);
Arrays.sort(A2);
//默认平局
int result = 0;
//判断是否cheating
HashMap<Integer, Integer> count = new HashMap<>();
for(int i=0;i<4;i++)
{
if(count.containsKey(A1[i]))
count.put(A1[i], count.get(A1[i])+1);
else
count.put(A1[i], 1);
if(count.containsKey(A2[i]))
count.put(A2[i], count.get(A2[i])+1);
else
count.put(A2[i], 1);
}
for(Integer i:count.values())
if(i>4)
result = -2;
//根据类型判断大小
if(result!=-2)
{
result = compare(A1, A2);
}
System.out.println(result);
}
public static int compare(int[] p1,int[] p2)
{
int result = 0;
int t1 = checkType(p1);
int t2 = checkType(p2);
if(t1<t2)
result = 1;
else if(t1>t2)
result = -1;
else
{
switch (t1) {
case 1:
if(p1[0]>p2[0])
result = 1;
else if(p1[0]<p2[0])
result = -1;
break;
case 2:
if(p1[2]>p2[2])
result = 1;
else if(p1[2]<p2[2])
result = -1;
break;
case 3:
int tmp1 = p1[0]==p1[1]?0:3;
int tmp2 = p2[0]==p2[1]?0:3;
if(p1[tmp1]>p2[tmp2])
result = 1;
else if(p1[tmp1]<p2[tmp2])
result = -1;
break;
case 4:
if(p1[3]>p2[3])
result = 1;
else if(p1[3]<p2[3])
result = -1;
else
{
if(p1[0]>p2[0])
result = 1;
else if(p1[0]<p2[0])
result = -1;
}
break;
case 5:
int temp1 = 0;
int temp2 = 0;
for(int i=1;i<4;i++)
{
if(p1[i]==p1[i-1])
temp1 = p1[i];
if(p2[i]==p2[i-1])
temp2 = p2[i];
}
if(temp1>temp2)
result = 1;
else if(temp1<temp2)
result = -1;
else {
int x=3,y=3,cnt=2;
while(cnt>0)
{
while(p1[x]==temp1)
x--;
while(p2[y]==temp2)
y--;
if(p1[x]>p2[y])
{
result = 1;
break;
}
else if(p1[x]<p2[y])
{
result = -1;
break;
}
else
{
x--;y--;
cnt--;
}
}
}
break;
case 6:
for(int i=3;i>=0;i--)
{
if(p1[i]>p2[i])
{
result = 1;
break;
}
else if(p1[i]<p2[i])
{
result = -1;
break;
}
}
break;
default:
break;
}
}
return result;
}
public static int checkType(int[] M)
{
int i = 1;
for(;i<4;i++)
if(M[i]!=M[i-1])
break;
if(i==4)
return 1;
if(M[3]==14)
{
if((M[0]==2&&M[1]==3&&M[2]==4)||(M[0]==11&&M[1]==12&&M[2]==13))
return 2;
}
else
{
for(;i<4;i++)
if(M[i]!=M[i-1]+1)
break;
if(i==4)
return 2;
}
if((M[0]==M[1]&&M[0]==M[2])||(M[3]==M[1]&&M[3]==M[2]))
return 3;
if(M[0]==M[1]&&M[2]==M[3])
return 4;
if((M[0]==M[1])||(M[1]==M[2])||(M[2]==M[3]))
return 5;
return 6;
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
public class Solution{
public static void main(String[] args) throws Exception{
/**/
String hand1;
String hand2;
InputStreamReader ir;
BufferedReader br;
ir = new InputStreamReader(System.in);
br = new BufferedReader(ir);
try
{
hand1 = br.readLine();
hand2 = br.readLine();
}
catch (IOException e) {
throw e;
}
HashMap<String, Integer> pokerValue = new HashMap<>();
for(int i=2;i<11;i++)
pokerValue.put(Integer.toString(i), i);
pokerValue.put("J", 11);
pokerValue.put("Q", 12);
pokerValue.put("K", 13);
pokerValue.put("A", 14);
int[] A1 = new int[4];
int[] A2 = new int[4];
String[] str = hand1.split(",");
for(int i=0;i<4;i++)
A1[i] = pokerValue.get(str[i]);
str = hand2.split(",");
for(int i=0;i<4;i++)
A2[i] = pokerValue.get(str[i]);
Arrays.sort(A1);
Arrays.sort(A2);
//默认平局
int result = 0;
//判断是否cheating
HashMap<Integer, Integer> count = new HashMap<>();
for(int i=0;i<4;i++)
{
if(count.containsKey(A1[i]))
count.put(A1[i], count.get(A1[i])+1);
else
count.put(A1[i], 1);
if(count.containsKey(A2[i]))
count.put(A2[i], count.get(A2[i])+1);
else
count.put(A2[i], 1);
}
for(Integer i:count.values())
if(i>4)
result = -2;
//根据类型判断大小
if(result!=-2)
{
result = compare(A1, A2);
}
System.out.println(result);
}
public static int compare(int[] p1,int[] p2)
{
int result = 0;
int t1 = checkType(p1);
int t2 = checkType(p2);
if(t1<t2)
result = 1;
else if(t1>t2)
result = -1;
else
{
switch (t1) {
case 1:
if(p1[0]>p2[0])
result = 1;
else if(p1[0]<p2[0])
result = -1;
break;
case 2:
if(p1[2]>p2[2])
result = 1;
else if(p1[2]<p2[2])
result = -1;
break;
case 3:
int tmp1 = p1[0]==p1[1]?0:3;
int tmp2 = p2[0]==p2[1]?0:3;
if(p1[tmp1]>p2[tmp2])
result = 1;
else if(p1[tmp1]<p2[tmp2])
result = -1;
break;
case 4:
if(p1[3]>p2[3])
result = 1;
else if(p1[3]<p2[3])
result = -1;
else
{
if(p1[0]>p2[0])
result = 1;
else if(p1[0]<p2[0])
result = -1;
}
break;
case 5:
int temp1 = 0;
int temp2 = 0;
for(int i=1;i<4;i++)
{
if(p1[i]==p1[i-1])
temp1 = p1[i];
if(p2[i]==p2[i-1])
temp2 = p2[i];
}
if(temp1>temp2)
result = 1;
else if(temp1<temp2)
result = -1;
else {
int x=3,y=3,cnt=2;
while(cnt>0)
{
while(p1[x]==temp1)
x--;
while(p2[y]==temp2)
y--;
if(p1[x]>p2[y])
{
result = 1;
break;
}
else if(p1[x]<p2[y])
{
result = -1;
break;
}
else
{
x--;y--;
cnt--;
}
}
}
break;
case 6:
for(int i=3;i>=0;i--)
{
if(p1[i]>p2[i])
{
result = 1;
break;
}
else if(p1[i]<p2[i])
{
result = -1;
break;
}
}
break;
default:
break;
}
}
return result;
}
public static int checkType(int[] M)
{
int i = 1;
for(;i<4;i++)
if(M[i]!=M[i-1])
break;
if(i==4)
return 1;
if(M[3]==14)
{
if((M[0]==2&&M[1]==3&&M[2]==4)||(M[0]==11&&M[1]==12&&M[2]==13))
return 2;
}
else
{
for(;i<4;i++)
if(M[i]!=M[i-1]+1)
break;
if(i==4)
return 2;
}
if((M[0]==M[1]&&M[0]==M[2])||(M[3]==M[1]&&M[3]==M[2]))
return 3;
if(M[0]==M[1]&&M[2]==M[3])
return 4;
if((M[0]==M[1])||(M[1]==M[2])||(M[2]==M[3]))
return 5;
return 6;
}
}