读书人

跪求好手解决急

发布时间: 2012-11-05 09:35:12 作者: rapoo

跪求高手解决——急急急
1、在一副扑克里(包括大小王),随便抽取5张牌,然后判断是不是顺子(这5张牌是否连续),大小王可以代替任何一张牌。
例如,1,2,3,5,大王,这五张牌就是一个顺子,并且J,Q,K,A,2也是一个顺子。


[解决办法]

C/C++ code
#include <stdio.h>#include <string.h>int main(){    char s[6][3],cnt[16];    int start,flag=1,hasJQK=0;    memset(cnt,0,sizeof(cnt));    for(int i=0;i<5;i++)    //大小王请输入0    {        scanf("%s",s[i]);        if(strcmp(s[i],"J")==0||strcmp(s[i],"Q")==0||strcmp(s[i],"K")==0)            hasJQK=1;        if(strlen(s[i])==2)            ++cnt[10];        else if(s[i][0]>='0'&&s[i][0]<='9')            ++cnt[s[i][0]-'0'];        if(s[i][0]=='J')            ++cnt[11];        else if(s[i][0]=='Q')            ++cnt[12];        else if(s[i][0]=='K')            ++cnt[13];        else if(s[i][0]=='A')            ++cnt[1],++cnt[14];        if(s[i][0]=='2')            ++cnt[15];        if(strcmp(s[i],"1")==0)    //1就是A??            ++cnt[14];    }    for(int i=1;i<16;i++)        if(cnt[i])        {            if(hasJQK&&(i==1||i==2))                continue;            start=i;            break;        }        flag=1;        for(int i=start;i<start+5&&i<16;i++)        {            if(cnt[i]>1)            {                flag=0;break;            }            else if(cnt[i]==0)            {                if(cnt[0]>0)                    --cnt[0];                else                {                    flag=0;break;                }            }        }        if(flag)            printf("是顺子\n");        else            printf("不是顺子\n");    }
[解决办法]
C/C++ code
#include <stdio.h>#include <time.h>#include <algorithm>// w 小王 W 大王char Pock[] = {'w' , 'W', 'A', '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K'};bool IsSort(int *a){    int total = 0;    int nW = 0;    for (int i=0; i<5; i++, nW++)    {        if( a[i] > 0)        {            break;        }    }    if (a[i] == 1 && a[4] == 12)    {        total++;    }    for (;i<4; i++)    {        if (a[i+1]-a[i] == 1)        {            total++;        }        else if(a[i+1]-a[i] <= nW)        {            int value = a[i+1]-a[i];            total +=  value;            nW -= value - 1;        }    }    total += nW;    printf("number = %d\n", total);    if (total == 4)    {        return true;    }    return false;}int main(int argc, char* argv[]){    char *pP = &Pock[1];    srand(time(0));    int a[5];    int i;    // 随机抽取,不考虑抽到 3 3 3 3 3的重复情况    for (i=0; i<5; i++)    {        a[i] = rand()%14 - 1; // -1,0 -大小王,10-J,11 Q,12 K     }    std::sort(&a[0], &a[5]);    for (i=0; i<5; i++)    {        printf("%c ", pP[a[i]]);    }    printf("\n");    if (IsSort(a))    {        printf("顺子\n");    }    return 0;}
[解决办法]
擦,忘记扑克牌有十了,还以为像十六进制一样10是J了。。。
C/C++ code
#include <stdio.h>#include <time.h>#include <algorithm>#include <windows.h>// w 小王 W 大王char Pock[] = {'w' , 'W', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'};bool IsSort(int *a){    int total = 0;    int nW = 0;    for (int i=0; i<5; i++, nW++)    {        if( a[i] > 0)        {            break;        }    }    if (a[i] == 1 && a[4] == 13)    {        total++;    }    for (;i<4; i++)    {        int value = a[i+1]-a[i];        if (value == 0)        {            continue;        }        if (value == 1)        {            total++;        }        else if(value-1 <= nW)        {                        total +=  value;            nW -= value - 1;        }    }    total += nW;    if (total == 4)    {        return true;    }    return false;}int main(int argc, char* argv[]){    char *pP = &Pock[1];    srand(time(0));    int a[5];    int i;    // 随机抽取,不考虑抽到 3 3 3 3 3的重复情况    do     {        for (i=0; i<5; i++)        {            a[i] = rand()%15 - 1; // -1,0 -大小王,11-J,12 Q,13 K         }        std::sort(&a[0], &a[5]);                // 如果是顺子打印        if (IsSort(a))        {            for (i=0; i<5; i++)            {                printf("%c ", pP[a[i]]);            }            printf("\n");        }        Sleep(50);    }while(true);    return 0;} 


[解决办法]
话说LZ另个帖子不是问过这个问题了么?

思路自己去看上个帖子,在下这里只帖代码(话说随机抽到顺子的概率真小……)。

PS:在下还真想不通J、Q、K、A、2是顺子,代码里没作这个判断。

C/C++ code
#include<stdio.h>#include<stdlib.h>#include<conio.h>#include<time.h>void deal(int*,int);void sort_poker(int*,int);void show_poker(int*,int);int is_straight(int*,int);int main(){    int po[5];    char con;    do    {        //deal(po,5);        //sort_pocker(po,5);        //printf("抽到的牌是:\n");        //show_pocker(po,5);        //printf("\n%s是顺子\n",is_straight(po,5)?"":"不");        do        {            deal(po,5);            sort_poker(po,5);        }while(!is_straight(po,5));        show_poker(po,5);        printf("再抽一组(Y/N)?");        con=getch();        printf("%c\n",con);    }while(con=='Y'||con=='y');    system("pause");    return(0);}void deal(int *a,int n)        //发牌{    register int i,j;    srand(time(NULL));    for(i=0;i<n;i++)    {        a[i]=rand()%54;        for(j=0;j<i;j++)            if(a[i]==a[j])            {                i--;                break;            }    }    //a[0]=23,a[1]=9,a[2]=52,a[3]=51,a[4]=37;        //这是一组成顺子的测试数据}void sort_poker(int *a,int n)        //扑克排序(在下偷懒,写个交换排序算了){    register int i,j;    int t=n-1;    for(i=0;i<t;i++)        for(j=i;j<n;j++)            if(a[j]>51||a[i]%13>a[j]%13)                a[i]^=a[j]^=a[i]^=a[j];}void show_poker(int *a,int n)        //输出扑克{    char color[]={3,4,5,6},pocker[][3]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};    register int i;    for(i=0;i<n;i++)    {        if(a[i]>51)            printf("%s Joker",a[i]&1?"R":"B");        else            printf("%c%s",color[a[i]/13],pocker[a[i]%13]);        printf("\t");    }}int is_straight(int *a,int n)        //判断是否顺子{    int omnipotent=0,cur1=0,cur2=13,i=0;    while(a[i]>51)        omnipotent++,i++;    cur1=(a[i++]+1)%13;    if(cur1==1)        cur2=9;    for(;i<n;i++)    {        if(a[i]%13==cur1)        {            cur1=(cur1+1)%13;            cur2=13;            continue;        }        if(a[i]%13==cur2)        {            cur2=(cur2+1)%13;            cur1=13;            continue;        }        if(omnipotent)        {            omnipotent--;            if(cur1<13)                cur1=(cur1+1)%13;            if(cur2<13)                cur2=(cur2+1)%13;            i--;        }        else            return(0);    }    return(1);} 

读书人网 >C语言

热点推荐