Saturday, January 4, 2014

10071: Back to High School Physics

I tried another uHunt "easy" problem. 10071: Back to High School Physics. It does not sound easy at all nor does it look like high school physics. The problem is about getting the distance an object travels given an initial time and initial velocity, and assuming that the particle travels at constant acceleration.

I really could not remember my physics anymore, so I tried to look at the net for the formulas. I attempted to derive the formulas using my super rusty calculus, but then I opted to look at the Internet for more accurate formulas. I found this formula:

v = at + v0

I then assumed that v0 = 0. Meaning, that the particle had zero velocity at time 0. I then computed for the acceleration at time t:

a = v / t

Then I also found another formula. It was something like:

r = 0.5 * a * (t^2) + (v0 * t) + r0

I then assumed that r0 = 0 and v0 is also 0. Substituting a = v / t, I ended up with the formula r = 2 * v * t
The problem asks the distance after twice the time needed to reach the velocity given in the problem, so:

r = 0.5 * a * (t^2)
r = 0.5 * (v / t)  * ((2t)^2)  // Problem asks the displacement after twice the time given in the problem.
r = 0.5 * (v / t) * (4 * t^2)
r = 2 * v * t

Later on, I realized that I did not really have to go through all these painful derivations. One can simply use the uVA toolkit to put some test inputs, and the take a look at the outputs. Based on the inputs and outputs, one can figure at the formula relating the input to the output. For instance the input 3,6 yields 36, while the input 5, 12 yields 120. So the output must be twice the product of the inputs. uVA toolkit can be found at http://uvatoolkit.com/  Again it is one of those more popular sites regarding uVA. It is also advertised in the wikipedia entry for uVA online judge along with uHunt.

Here's my sample code:

#include

int main() {
  char line[100];
  int v, t, r;

  while (fgets(line, 100, stdin)) {
    sscanf(line, "%d %d", &v, &t);

    r = (v * t);
    r = r << 1;
    
    printf("%d\n", r);
  }
  
  return 0;
}

No comments:

Post a Comment