Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include<limits>
  3. #include <algorithm>
  4. #include <iostream>
  5. using namespace std;
  6. struct Bucket{
  7.     float left;
  8.     float right;
  9.     Bucket():left(0),  right(1) {}
  10.     bool isWet() {
  11.         return left - right > 1e-5;
  12.     }
  13.     void update(float L,  float R) {
  14.         if (L <= 0.0) left = max(left,  R);
  15.         if (R >= 1.0) right = min(right,  L);
  16.     }
  17. };
  18.  
  19. void simulate() {
  20.     int N = 100;
  21.     int wet_num = 0;
  22.     Bucket arr[100];
  23.     int num = 0;
  24.     while(wet_num < 100) {
  25.         num += 1;
  26.         float x = rand() * 1.0 / INT_MAX * 100;
  27.         int right = min(99, (int)(x  + 0.5));
  28.         int left = (int) (x -0.5);
  29.         if (!arr[left].isWet()) {
  30.             arr[left].update(x  - 0.5 - left,  x - left + 0.5);
  31.             wet_num+= arr[left].isWet();
  32.         }
  33.         if (!arr[right].isWet()) {
  34.             arr[right].update(x - right - 0.5,  x - right + 0.5);
  35.             wet_num += arr[right].isWet();
  36.         }
  37.     }
  38.     cout << num << endl;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement