亞馬遜2015校招在線筆試2

Q1.png
這道題真是虐心。總的路線是先判斷撲克的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;
     }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章