C语言学习: 数组打乱

 1 #include <stdio.h>
 2 #include "io_utils.h"
 3 #include <stdlib.h>
 4 #include <time.h>
 5 
 6 #define PLAYER_COUNT 50
 7 
 8 void SwapElement(int array[], int first, int second){
 9   int temp=array[first];
10   array[first]=array[second];
11   array[second]=temp;
12 }
13 void Function_suf(int array[],int size){
14   srand(time(NULL));
15   for (int i = 0; i < size; ++i) {
16     int rand_number=rand()%size;
17     SwapElement(array,i,rand_number);
18   }
19 }
20 
21 void Function_suf1(int array[],int size){
22   srand(time(NULL));
23   for (int i = size; i >0; i--) {
24     int rand_number=rand()%i;
25     SwapElement(array,i,rand_number);
26   }
27 }
28 
29 int main() {
30   int players[PLAYER_COUNT];
31   for (int i = 0; i < PLAYER_COUNT; ++i) {
32     players[i]=i;
33   }
34 
35   PRINT_INT_ARRAY(players,PLAYER_COUNT);
36   Function_suf1(players,PLAYER_COUNT);
37   PRINT_INT_ARRAY(players,PLAYER_COUNT);
38   return 0;
39 }
View Code

 

posted @ 2023-02-02 22:21  泥古拉斯赵四  阅读(16)  评论(0编辑  收藏  举报