Sicily 1018. A Card Trick

1018. A Card Trick

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge

Description

The following card trick is performed by a Magician and her Assistant. The Assistant asks a member of the audience to choose 5 cards from a standard deck of 52 cards (A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K of C[lubs], D[iamonds], H[earts] and S[pades]). The Assistant returns one of the cards to the audience member and then hands the remaining cards to the Magician, one at a time. After suitable mumbo-jumbo, the Magician identifies the (fifth) card held by the audience member.   

The Magician determines the card as follows:   

The order of the cards in the deck is determined first by the value and for cards of the same value by the suit (both in the order given above). So the total order of cards is: AC, AD, AH, AS, 2D, … , KH, KS   

  1. Remember the suit and value of the first card.  
  2. Among the remaining three cards find the position of the smallest card (in the above order). Add this position (1, 2, or 3) to the value of the first card.  
  3. If the larger two of the last three cards are not in order, add 3 to the result of step 2.  
  4. The missing card has the same suit as the first card and value that computed in step 3 wrapping around if necessary.    

For example:   

QH, 10D, 10C, 4D   

Smallest of the last 3 cards is 4D in place 3. 10D and 10C are out of order so add 3 + 3 to Q. Wrapping around the missing card is 5H.  

This problem is to write a program to perform the function of the Assistant.

Input

The first line of the input file named j.in consists of a positive integer n, which is the number of datasets that follow. Each of the n following lines contain one data set. The dataset is a sequence of 5 cards separated by a space. Each card is given by a one or two character value and a one character suit as described in the first paragraph.

Output

For each dataset, the output on separate lines is an ordering of the 5 input cards as shown in the sample output. The first card in the sequence is the card to be returned to the audience member. The remaining cards are those given to the Magician (in the order given to the Magician). There may be more than one solution to some problems (but that is not to say there *will* be). In cases such as these, any of the correct solutions will be accepted by the judges. For instance 10D 4D QH 10C 5H is also a solution to the first problem below.

Sample Input

2						
4D 5H 10C 10D QH			
7H 5C KS 6C 8D 

Sample Output

Problem 1: 5H QH 10D 10C 4D
Problem 2: 6C 5C 7H 8D KS

模拟题:

#include <stdio.h>
#include <algorithm>
#define MAX 5 * 4 * 3 * 2 * 1
using namespace std;

int data[5][2];//读入的数据
int to_check[5][2];//按照排列生成的数据
int permutation[MAX][5];//排列

int card_min(int a, int b) {
    if (to_check[a][0] != to_check[b][0]) {
        return to_check[a][0] < to_check[b][0] ? a : b;
    } else {
        return to_check[a][1] < to_check[b][1] ? a : b;
    }
}
    

void make_permutation() {
    int temp[5];
    int j = 1;
    for (int i = 0; i < 5; i++) {
        permutation[0][i] = i;
        temp[i] = i;
    }
    while (next_permutation(temp, temp + 5)) {//这个函数以前我研究过一点点
        for (int i = 0; i < 5; i++) {
            permutation[j][i] = temp[i];
        }
        j++;
    }
}

void input() {
    char temp[5];
    for (int i = 0; i < 5; i++) {
        scanf("%s", &temp);
        if (temp[0] == '1' && temp[1] == '0') {
            data[i][0] = 9;
            data[i][1] = temp[2];
        } else if (temp[0] == 'A') {
            data[i][0] = 0;
            data[i][1] = temp[1];
        } else if (temp[0] == 'J') {
            data[i][0] = 10;
            data[i][1] = temp[1];
        } else if (temp[0] == 'Q') {
            data[i][0] = 11;
            data[i][1] = temp[1];
        } else if (temp[0] == 'K') {
            data[i][0] = 12;
            data[i][1] = temp[1];
        } else {
            data[i][0] = temp[0] - '1';
            data[i][1] = temp[1];
        }
    }
}

bool check() {//检查是否ok
    if (to_check[1][1] != to_check[0][1])
        return false;
    int temp = to_check[1][0];
    int smallest = card_min(card_min(2, 3), 4);
    to_check[1][0] = (to_check[1][0] + smallest - 1) % 13;
    int before = -1, after = -1;
    for (int i = 2; i < 5; i++) {
        if (i != smallest && before == -1) {
            before = i;
            continue;
        }
        if (i != smallest && after == -1) {
            after = i;
        }
    }
    if (card_min(before, after) != before) {
        to_check[1][0] = (to_check[1][0] + 3) % 13;
    }
    if (to_check[0][0] == to_check[1][0] && to_check[0][1] == to_check[1][1]) {
        to_check[1][0] = temp;
        return true;
    }
    else
        return false;
}

void output(int k) {
    printf("Problem %d:", k);
    for (int i = 0; i < 5; i++) {
        if (to_check[i][0] == 0) {
            printf(" A");
        } else if (to_check[i][0] == 10) {
            printf(" J");
        } else if (to_check[i][0] == 11) {
            printf(" Q");
        } else if (to_check[i][0] == 12) {
            printf(" K");
        } else {
            printf(" %d", to_check[i][0] + 1);
        }
        printf("%c", to_check[i][1]);
    }
    printf("\n");
}
    

int main() {
    int case_num;
    make_permutation();
    scanf("%d", &case_num);
    for (int k = 1; k <= case_num; k++) {
        bool is_ok = false;
        input();
        for (int i = 0; i < MAX && !is_ok; i++) {
            for (int j = 0; j < 5; j++) {
                to_check[j][0] = data[permutation[i][j]][0];
                to_check[j][1] = data[permutation[i][j]][1];
            }
            is_ok = check();
        }
        output(k);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值