Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3.  
  4. void shuffle_swap(int *arr, int n) {
  5.     for (int i = 0; i < 10 * n; ++i) {
  6.         int pos1 = rand() % n;
  7.         int pos2 = rand() % n;
  8.         int t = arr[pos1];
  9.         arr[pos1] = arr[pos2];
  10.         arr[pos2] = t;
  11.     }
  12. }
  13.  
  14. void shuffle_rev(int *arr, int n) {
  15.     for (int i = 0; i < 10 * n; ++i) {
  16.         int pos1 = rand() % n;
  17.         int pos2 = rand() % n;
  18.         if (pos1 > pos2) {
  19.             int t = pos1;
  20.             pos1 = pos2;
  21.             pos2 = t;
  22.         }
  23.         for (int j = pos1; j <= pos1 + (pos2 - pos1) / 2; ++j) {
  24.             int t = arr[j];
  25.             arr[j] = arr[pos2 - (j - pos1)];
  26.             arr[pos2 - (j - pos1)] = t;
  27.         }
  28.     }
  29. }
  30.  
  31. int main() {
  32.     srand(time(0));
  33.     int a[20];
  34.     int b[20];
  35.     for (int i = 0; i < 20; ++i) {
  36.         a[i] = i;
  37.         b[i] = i;
  38.     }
  39.     shuffle_swap(a, 20);
  40.     shuffle_rev(b, 20);
  41.     for (int i = 0; i < 20; ++i) {
  42.         printf("%d ", a[i]);
  43.     }
  44.     printf("\n");
  45.     for (int i = 0; i < 20; ++i) {
  46.         printf("%d ", b[i]);
  47.     }
  48.     printf("\n");
  49. }
  50.  
  51.  
  52.  
  53.  
  54. #include "stdio.h"
  55. #include "stdlib.h"
  56. #include "time.h"
  57.  
  58. const int MY_RANDMAX = (1 << 15) - 1;
  59.  
  60. int superRand() {
  61.     return (rand() << 15) + (rand() + rand()) % (1 << 15);
  62. }
  63.  
  64. int main() {
  65.     srand(time(0));
  66.     int cnt[MY_RANDMAX + 1];
  67.     memset(cnt, 0, sizeof(cnt));
  68.     for (int i = 0; i < MY_RANDMAX + 1; ++i) {
  69.         cnt[superRand() % (MY_RANDMAX + 1)]++;
  70.     }
  71.     int cnt_[10];
  72.     memset(cnt_, 0, sizeof(cnt_));
  73.     int strange = 0;
  74.     for (int i = 0; i < MY_RANDMAX + 1; ++i) {
  75.         if (cnt[i] < 10) {
  76.             cnt_[cnt[i]]++;
  77.             if (cnt[i] >= 7) {
  78.                 printf("Number %d occured %d times\n", i, cnt[i]);
  79.             }
  80.         } else {
  81.             strange++;
  82.         }
  83.     }
  84.     for (int i = 0; i < 10; ++i) {
  85.         printf("There are %d numbers that occured %d times\n", cnt_[i], i);
  86.     }
  87.     printf("There are %d strange numbers\n", strange);
  88.     //printf("%d %d\n", RAND_MAX, (1 << 15) - 1);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement