Advertisement
daltond124

Untitled

Sep 30th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define EPS 1e-9
  4. #define TRUE (1<2)
  5. #define FALSE (!TRUE)
  6. // The function that checks if two values are equal
  7. int eq (double a, double b);
  8. // The function that searchs from the lo to the hi for the max area
  9. // The function return the max area found
  10. double search(double lo, double hi, double length);
  11. // The function that returns the area (could be repurposed for Moore)
  12. double foo(double total, double upper);
  13. // The main function
  14. int main(){
  15. // Read in the length
  16. int i, numCases;
  17. scanf("%d", &numCases);
  18. for (i=0; i < numCases; i++){
  19. double length;
  20. scanf("%lf ", &length);
  21.  
  22. // Initialize the lo and hi bound of the top rod of the frame
  23. double lo = 0;
  24. double hi = 1;
  25. // Check if we can use a better upper bound for the length of the top rod
  26. while (foo(length, hi) < foo(length, hi * 2))
  27. hi = hi * 2;
  28. // Double one last time to ensure we have the maximum within our range
  29. hi = hi * 2;
  30. // Compute the answer by searching from the lo to the hi bound
  31. double ans = search(lo, hi, length);
  32. // Print the answer
  33. printf("%lf\n", ans);
  34. // Return the answer
  35.  
  36. return 0;
  37. }
  38. }
  39. double search(double lo, double hi, double length)
  40. {
  41. // Check if the lo and hi value are approximately the same
  42. if (eq(lo, hi))
  43. return foo(length, lo); // Get the area of the frame
  44. // Compute the mid points using the 2 thirds trick discussed in class
  45. // Note this breaks the search space into 3 "equal" parts
  46. double m1 = (2 * lo + hi) / 3;
  47. double m2 = (lo + 2 * hi) / 3;
  48. // Compute the area using the first and second mid points respectively
  49. double area1 = foo(length, m1);
  50. double area2 = foo(length, m2);
  51. // Check if area 1 is worse than area 2
  52. if (area1 < area2) {
  53. // Since area 1 is worse eliminate the first section of the search move up the lower bound lo = m1;
  54. return search(m1, hi, length);
  55.  
  56. }
  57. // Area 1 is better move down the hi bound
  58. return search(lo, m2, length);
  59. }
  60.  
  61. int eq (double a, double b){ // Get the difference
  62. double diff = a - b;
  63. if (diff < 0)
  64. diff = -diff; // absolute value of the difference
  65. if (diff < EPS)
  66. return TRUE; // ABSOLUTE ERROR CHECK PASSED
  67. if (a < 0)
  68. a = -a; // abs
  69. if (b < 0)
  70. b = -b; // abs
  71. if (a < b)
  72. a = b; // max
  73. // Scale the error by the magnitude of the big value
  74. // This will give a new relative error to check against
  75. if (a * EPS > diff)
  76. return TRUE; // RELATIVE ERROR CHECK PASSED
  77.  
  78. return FALSE; // Not equal values
  79. }
  80.  
  81. // This function computes the area of the frame given
  82. // 1. The total length of the wooden rod
  83. //2. The width of the frame (upper wood piece)
  84. double foo(double total, double upper){
  85. // Compute the remaining length after using the bottom and top
  86. double result, n;
  87. result = total / pow(upper, sqrt(n)) + n;
  88. //result = k / pow(H, sqrt(n)) + n;
  89. //double remainingRod = total - 2 * upper;
  90. // Compute the height by using the remaining rod in 6 different pieces
  91. //double small = remainingRod / 6;
  92. // Return the area (product of the height and the width)
  93. //return small * upper;
  94. return result;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement