ACM篇:POJ 2200 --A Card Trick

来源:互联网 发布:摄像头扫描软件 编辑:程序博客网 时间:2024/06/11 00:36

纸牌模拟是最恶心的模拟,没有之一。

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <cmath>using namespace std;int card[5];int temp[5];int readchar(){    int t;    while (t=getchar())    {        if (isdigit(t) || isalpha(t)) break;    }    return t;}const char VALUES[] = " A234567890JQK";const char SUITS[] = " CDHS";int _turn(int x, bool is_in, bool is_value){    if (is_in)    {        if (x == '1')        {            getchar();            return 100;        }        for (int i = 1; VALUES[i] != '\0'; i++)        {            if (VALUES[i] == x) return i*10;        }        for (int i = 1; SUITS[i] != '\0'; i++)        {            if (SUITS[i] == x) return i;        }    }    else     {        if (is_value)        {            if (x == 10) putchar('1');            return VALUES[x];        }        else return SUITS[x];    }}void readcard(){    int t;    for (int i = 0; i < 5; i++)    {        card[i] += _turn(readchar(), true, false);        card[i] += _turn(readchar(), true, false);    }}bool is_ok(int t[],int &d){    d = t[0]-t[1];    if (d % 10) return false;           // the same suit    d /= 10;    if (d < 0) d += 13;    if (d >= 1 && d <= 6) return true;    else return false;}void _print(int t[], int d, int kase){    if ((d-1) % 3 == 1)    {        swap(t[2], t[3]);    }    else if ((d-1) % 3 == 2)    {        swap(t[2], t[4]);        if (d == 3) swap(t[2], t[3]);    }    if (d == 4) swap(t[3], t[4]);    if (d == 5) swap(t[2], t[4]);    printf("Problem %d:", kase);    for (int i = 0; i < 5; i++)    {        putchar(' ');        putchar(_turn(t[i]/10, false, true));        putchar(_turn(t[i]%10, false, false));    }    putchar('\n');}int main(){    int T;    int kase = 0;    scanf("%d", &T);    while (++kase <= T)    {        bool mark = false;        memset(card, 0, sizeof(card));        readcard();        sort(card, card+5);        for (int i = 0; i < 5 && !mark; i++)            for (int j = 0; j < 5 && !mark; j++)            {                if (i == j) continue;                int cnt = 2;                for (int k = 0; k < 5; k++)                {                    if (k == i) temp[0] = card[i];                    else if (k == j) temp[1] = card[j];                    else temp[cnt++] = card[k];                 }                int d;                if(is_ok(temp, d))                {                    _print(temp, d, kase);                    mark = true;                }            }    }    return 0;} 
0 0
原创粉丝点击