fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int main(void) {
  6. int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  7. int index, last, temp;
  8. srand((unsigned int)time(NULL));
  9. last = 9;
  10. index = rand() % 10;
  11. temp = a[last];
  12. a[last] = a[index];
  13. a[index] = temp;
  14.  
  15. printf("交换后的数组:");
  16. for (int i = 0; i < 10; i++) {
  17. printf("%d ", a[i]);
  18. }
  19. printf("\n");
  20.  
  21. return 0;
  22. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
交换后的数组:1 10 3 4 5 6 7 8 9 2