这种洗牌方式的算法:

1:初始化一个长度为54的数组,和54张牌。

2:从i=1开始到54,每次从剩余的牌堆中随机抽一张牌放到数组的i下标下。

这样我们可以用一个数组做也可以用两个数组做。

就和经典的选择排序一样。

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<algorithm>
#define POKER_NUM 54
int poker[54];
void iniPoker()
{
    for(int i=0;i<POKER_NUM;i++)
    {
        poker[i]=i+1;
    }
}
void suffle()
{
    for(int i=0;i<POKER_NUM;i++)
    {
        int index=rand()%(POKER_NUM-i)+i;         //获取从i~POKER_NUM的一个索引
        std::swap(poker[i],poker[index]);        //交换
    }
}
void printPoker()
{
    for(int i=0;i<POKER_NUM;i++)
    {
        if(i%9==0) printf("\n");
        printf("%4d",poker[i]);
    }
}
int main()
{
    srand(time(NULL));
    iniPoker();
    printPoker();
    suffle();
    printf("\n-------------------------------------------------------------\n");
    printPoker();
}




 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-16
  • 2021-11-29
  • 2021-11-17
  • 2021-11-17
猜你喜欢
  • 2021-07-15
  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
  • 2021-05-26
  • 2021-11-17
  • 2021-06-03
相关资源
相似解决方案