Guest User

Untitled

a guest
Jun 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <time.h>
  3. #include "iostream"
  4. #include <math.h>
  5. using namespace std;
  6. double p;
  7. //n is the length of a car
  8. double n;
  9. double e = 0.0000001;
  10. //double e = 0;
  11. int interval(double l);
  12. int interval(double l, double a){
  13. a = a - floor(a);
  14. //Length < 1.0
  15. if(l<n-e) return 0;
  16.  
  17. double t = ((double) rand())/((double) RAND_MAX);
  18.  
  19. if(t>p){
  20. //bad driver
  21. t = ((double) rand())/((double) RAND_MAX)*(l-n);
  22. return interval(t,a) + interval(l-t-n,a-t) + 1;
  23. }else{
  24. //good driver
  25. if(l-a<n){
  26. //the car has to park behind another car
  27. //Clearly the interval l can be treated as having no lines
  28. //break into 2 smaller pieces
  29. return interval(l-n)+1;
  30. }else{
  31. t = floor(((double) rand())/((double) RAND_MAX)*floor(l-n-a));
  32. return interval(t+a,a) + interval(l-a-t-n,1-n) + 1;
  33. }
  34. }
  35. }
  36. int interval(double l){
  37. if(l<n-e){
  38. return 0;
  39. }
  40. double t = ((double) rand())/((double) RAND_MAX);
  41. if(t>p){
  42. t = ((double) rand())/((double) RAND_MAX)*(l-n);
  43. return interval(t) + interval(l-t-n) + 1;
  44. }else{
  45. return interval(l-n)+1;
  46. }
  47. }
  48. int main(int argc, char* argv[]) {
  49. srand(time(NULL));
  50. double l = atof(argv[1]);
  51. p = atof(argv[2]);
  52. //int z = atoi(argv[3]);
  53. double z = atof(argv[3]);
  54. n = (1.0 / (double) z);
  55. int itr = atoi(argv[4]);
  56. long t=0;
  57. l=l/z;
  58. for(int i=0;i<itr;i++){
  59. t+= interval(l,0);
  60. }
  61. cout << ((double) t )/ ((double) itr )/ l * n << endl;
  62. return 0;
  63. }
Add Comment
Please, Sign In to add comment