Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include "stdlib.h"
- void shuffle_swap(int *arr, int n) {
- for (int i = 0; i < 10 * n; ++i) {
- int pos1 = rand() % n;
- int pos2 = rand() % n;
- int t = arr[pos1];
- arr[pos1] = arr[pos2];
- arr[pos2] = t;
- }
- }
- void shuffle_rev(int *arr, int n) {
- for (int i = 0; i < 10 * n; ++i) {
- int pos1 = rand() % n;
- int pos2 = rand() % n;
- if (pos1 > pos2) {
- int t = pos1;
- pos1 = pos2;
- pos2 = t;
- }
- for (int j = pos1; j <= pos1 + (pos2 - pos1) / 2; ++j) {
- int t = arr[j];
- arr[j] = arr[pos2 - (j - pos1)];
- arr[pos2 - (j - pos1)] = t;
- }
- }
- }
- int main() {
- srand(time(0));
- int a[20];
- int b[20];
- for (int i = 0; i < 20; ++i) {
- a[i] = i;
- b[i] = i;
- }
- shuffle_swap(a, 20);
- shuffle_rev(b, 20);
- for (int i = 0; i < 20; ++i) {
- printf("%d ", a[i]);
- }
- printf("\n");
- for (int i = 0; i < 20; ++i) {
- printf("%d ", b[i]);
- }
- printf("\n");
- }
- #include "stdio.h"
- #include "stdlib.h"
- #include "time.h"
- const int MY_RANDMAX = (1 << 15) - 1;
- int superRand() {
- return (rand() << 15) + (rand() + rand()) % (1 << 15);
- }
- int main() {
- srand(time(0));
- int cnt[MY_RANDMAX + 1];
- memset(cnt, 0, sizeof(cnt));
- for (int i = 0; i < MY_RANDMAX + 1; ++i) {
- cnt[superRand() % (MY_RANDMAX + 1)]++;
- }
- int cnt_[10];
- memset(cnt_, 0, sizeof(cnt_));
- int strange = 0;
- for (int i = 0; i < MY_RANDMAX + 1; ++i) {
- if (cnt[i] < 10) {
- cnt_[cnt[i]]++;
- if (cnt[i] >= 7) {
- printf("Number %d occured %d times\n", i, cnt[i]);
- }
- } else {
- strange++;
- }
- }
- for (int i = 0; i < 10; ++i) {
- printf("There are %d numbers that occured %d times\n", cnt_[i], i);
- }
- printf("There are %d strange numbers\n", strange);
- //printf("%d %d\n", RAND_MAX, (1 << 15) - 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement