在0-9中随机抽取四个数,不可重复,次序随机

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

void slashCard(int arr[]){
 srand((unsigned int)time(NULL));
 int pool[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 int s;
 int i;
 for (i = 0; i < 4; i++){
  s = rand() % 10;   //随机抽取
  while (pool[s] == 1){
   s++;
   if (s == 10){    //防止越界
    s = 0;
   }
  }
  arr[i] = pool[s];   //取出有效的s
  pool[s] = -1;
 }
}

void printfArray(int arr[], int n){
 int i;
 for (i = 0; i < n; i++){
  printf("%d", arr[i]);
 }
 putchar('\n');
}

int main()
{
 int arr[4];
 slashCard(arr);
 printfArray(arr,4);
 Sleep(1000);   //消除伪随机
 system("pause");
 return 0;
}

猜你喜欢

转载自www.cnblogs.com/Leafbud/p/12591058.html
今日推荐