照着题目的输入是正确的,但是我没有测试过,但是提交上去是错误的,可能本人技术比较烂吧,提交的题目都是Wrong answer
Problem D
Poker Hands
Input: standard input
Output: standardoutput
Time Limit: 2 seconds
Memory Limit: 32 MB
A poker deck contains 52 cards - each card has a suit whichis one of clubs, diamonds, hearts, or spades (denoted C, D, H, and S in the input data). Each card also has a value which is one of 2, 3,4, 5, 6, 7, 8,9, 10, jack, queen, king, ace (denoted 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A).For scoring purposes, the suits are unordered while the values are ordered asgiven above, with 2 being the lowest and ace the highest value.
A poker hand consists of 5 cards dealt from the deck. Poker hands are ranked by thefollowing partial order from lowest to highest
Your job is to compare several pairs of pokerhands and to indicate which, if either, has a higher rank.
Input
The input file contains severallines, each containing the designation of 10cards: the first 5 cards are thehand for the player named "Black"and the next 5 cards are the handfor the player named "White".
Output
For each line of input, print a line containing one of thefollowing three lines:
Black wins.
White wins.
Tie.
Sample Input
2H 3D 5S 9C KD 2C 3H 4S 8C AH
2H 4S 4C 2D 4H 2S 8S AS QS 3S
2H 3D 5S 9C KD 2C 3H 4S 8C KH
2H 3D 5S 9C KD 2D 3H 5C 9S KH
Sample Output
White wins.
Black wins.
Black wins.
Tie.
我写的代码:
#include <stdio.h> #include <algorithm> #include <cassert> #include <cstring> using namespace std; #define NCARDS 52 #define NSUITS 4 char values[] = "23456789TJQKA"; char types[] = "CDHS"; char white[] = "White wins."; char black[] = "Black wins."; char tie [] = "Tie."; int rank_card(char value , char type) { int i,j; for (i = 0 ; i < NCARDS; ++i ) { if (value == values[i]) { for (j = 0 ; j < NSUITS ; ++j) { if (type == types[j]) { return (i*NSUITS+j); } } } } fprintf(stderr,"bad card\n"); return -1; } char card_type(int card) { return types[card%4]; } char card_value(int card) { return values[card/4]; } bool is_order(int arr[]) { char str[6] = ""; int i = 0; for( ; i < 5 ; i++) str[i] = card_value(arr[i]); char* rc = search(values,values+13,str,str+5); return (rc!=values+13); } bool is_same_type(int arr[]) { int i = 1; for(; i < 5 ; i ++) { if(card_type(arr[i]) != card_type(arr[i-1])) return false; } return true; } int get_level_type(int arr[]) { char ch = card_value(arr[0]); int level_type = 0; int i ; int nsame= 1; int ndouble = 0 , nthree = 0 , nfour = 0; for(i = 1; i < 5 ; ++i) { if(ch == card_value(arr[i])) { nsame++; } else { ch = card_value(arr[i]); if(nsame == 2) { ndouble++; } else if(nsame == 3) { nthree++; } else if (nsame == 4) { nfour++; } nsame = 1; } } if(nsame == 2) { ndouble++; } else if(nsame == 3) { nthree++; } else if (nsame == 4) { nfour++; } assert(!(nthree&&nfour)); assert(ndouble < 3); if(nfour) { level_type = (1<<6); return level_type; } if(ndouble == 2)//双对 { level_type = ndouble; return level_type; } else if(ndouble ==1)//一对 { printf("yi dui \n"); level_type = ndouble; } if(nthree)//三张 { level_type |= (1<<2); if(ndouble ==1) { level_type |= (1<<5);//葫芦 } return level_type; } if(ndouble) //如果 有一对 则不用检查 顺子和同花; return level_type; if(is_same_type(arr))//同花 { level_type = (1<<4); } if (is_order(arr))//顺子 { level_type |= (1<<3); } if( (level_type & (1<<4)) && (level_type & (1<<3)) ) { level_type |= (1<<7); } return level_type; } int same_level_cmp(int arr1[],int level_type1 , int arr2[],int leve_type2) { int i = 0; int arr1_1,arr1_2,arr2_1,arr2_2;//检测双对 if( !level_type1 || (level_type1&(1 << 4)) || (level_type1&(1<<3))) //同花 顺子 最大牌 归为一类 , 选择两幅牌的最大牌 { for(i = 4 ; i >=0 ; i--) { if (card_value(arr1[i]) != card_value(arr2[i])) { return arr2[i]-arr1[i]; } } } if( (level_type1&(1<<6)) || (level_type1&(1<<2))) //四张 三张(包括葫芦) 归为一类 只要检测五张牌的中间那张 { printf("duo zhang"); return arr2[2] - arr1[2]; } if( (level_type1&(1<<1)) ) //双对 { printf("shuang dui\n"); arr1_1 = arr1[1]; arr1_2 = arr1[3]; if(arr1_1 > arr1_2) swap(arr1_1,arr1_2); arr2_1 = arr2[1]; arr2_2 = arr2[3]; if (arr2_1 > arr2_2) swap(arr2_1,arr2_2); if(arr1_2 != arr2_2) return arr2_2 - arr1_2; else return arr2_1 - arr1_1; } if( (level_type1&(1)) )//一对 { printf("yi dui\n"); for (i = 1 ; i < 5 ; i ++) { if (card_value(arr1[i]) == card_value(arr1[i-1])) { arr1_1 = arr1[i]; break; } } for (i = 1; i < 5 ; i ++) { if(card_value(arr2[i]) == card_value(arr2[i-1])) { arr2_1 = arr2[i]; break; } } if(arr1_1 != arr2_1) return arr1_1 - arr2_1; for (i = 4 ; i >= 0 ; i--) { if(card_value(arr1[i]) != card_value(arr2[i])) return arr2[i] - arr1[i]; } } return 0; } int get_result(int arr1[] , int arr2[]) { int level_type1 = 0 , level_type2 = 0; sort(arr1,arr1+5); sort(arr2,arr2+5); level_type1 = get_level_type(arr1); level_type2 = get_level_type(arr2); if (level_type1 > level_type2) { return -1; } else if(level_type1 < level_type2) { return 1; } return same_level_cmp( arr1,level_type1 , arr2, level_type2); } int main() { char str[1000]; char *p = NULL; int i = 0; int result = 0; memset(str,0,1000); int array[10] = {0}; while(gets(str)) { i = 0; p = strtok(str," "); while (p) { array[i++] = rank_card(p[0],p[1]); p = strtok(NULL," "); } // result =get_result(array,array+5); if ( ( result=get_result(array,array+5)) < 0 ) { printf("%s\n",black); } else if(result > 0) { printf("%s\n",white); } else { printf("%s\n",tie); } memset(str,0,1000); } return 0; } //2H 3D 5S 9C KD 2C 3H 4S 8C AH //2H 4S 4C 2D 4H 2S 8S AS QS 3S //2H 3D 5S 9C KD 2C 3H 4S 8C KH //2H 3D 5S 9C KD 2D 3H 5C 9S KH // 2C 2D 2S 2H 3S 5S 6S 7D 8S AS