Speed Limit time limit:2 Seconds Memory limit:65536 KB
Bill and Ted is taking a road trip. But the odometer in their car was broken, so they don ' t know how many miles they has driven. Fortunately, Bill had a working stopwatch, so they can record their speed and the total time they had driven. Unfortunately, their record keeping strategy is a little odd, so they need help computing the total distance driven. You is to write a program to does this computation.
For example, if their log shows
Speed in miles per hour |
Total elapsed time in hours |
20 |
2 |
30 |
6 |
10 |
7 |
This means they drove 2 hours at $ miles per hour, then 6-2=4 hours at $ miles per hour, then 7-6=1 hour at ten miles per Hour. The distance driven is then (2) (+) + (4) (+) + (1) (Ten) = + + + + Note The total elapsed time was always since the beginning of the trip, not since the previous entry in their log.
Input
The input consists of one or more data sets. Each set starts with a line containing an integer n, 1≤n≤10, followed by n pairs of values, one pair per line. The first value in a pair, s, was the speed in miles per hour and the second value, T, was the total elapsed time. Both s and t are integers, 1≤s≤90 and 1≤t≤12. The values for T is always in strictly increasing order. A value of-1 for n signals the end of the input.
Output
For each of the input set, print the distance driven, followed by a space, and followed by the word "Miles".
Example Input: |
Example output: |
3 20 2 30 6 10 7 2 60 1 30 5 4 15 1 25 2 30 3 10 5 -1 |
Miles + Miles All Miles
|
Test instructions: Calculates the total distance at different speeds, in separate periods
Code:
#include <stdio.h>
int main ()
{
int N;
while (scanf ("%d", &n))
{
if (n==-1)
Break
int i;
int s,t,temp,mile=0;
for (i=0;i<n;i++)
{
scanf ("%d%d", &s,&t);
if (i==0)
{
Mile+=s*t;
temp=t;
}
Else
{
mile+= (t-temp) *s;
temp=t;
}
}
printf ("%d miles\n", mile);
}
return 0;
}