用随机数给一个二维数组赋值 要求不能有重复的数据
方法一:
1 #include <stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 int main() 5 { 6 srand((unsigned)time(NULL)); 7 int arr[100]; 8 for (int i = 0; i < 100; i++) 9 { 10 arr[i] = rand() % 200; 11 for (int j = 0; j <i; j++) 12 { 13 if (arr[j]==arr[i]) 14 { 15 --i; 16 break; 17 } 18 } 19 } 20 for (int i = 0; i < 100; i++) 21 { 22 printf("%d ", arr[i]); 23 } 24 getchar(); 25 return 0; 26 }
方法二:
1 #include <stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 int main() 5 { 6 srand((unsigned)time(NULL)); 7 int arr[100]; 8 for (int i = 0; i < 100; i++) 9 { 10 arr[i] = i; 11 } 12 int temp, m; 13 for (int j = 99; j > 0; j--) 14 { 15 m = rand() % j; 16 temp = arr[j]; 17 arr[j] = arr[m]; 18 arr[m] = temp; 19 } 20 for (int i = 0; i < 100; i++) 21 { 22 printf("%d ", arr[i]); 23 } 24 getchar(); 25 return 0; 26 }