2017 Speed Limit: 0ms AC small water problem

Title Translation

Bill and Ted ongoing travel. But their car odometer is broken, so they do not know how many miles traveled. Fortunately, Bill stopwatch function properly, so they can record their speed and total driving time. Unfortunately, their record keeping strategy a bit strange, so they need help to calculate the total distance traveled. You will write a program to perform this calculation.
Here Insert Picture Description
This means that they are traveling at a speed of 20 miles per hour for 2 hours and then traveling at a speed of 30 miles per hour 6-2 = 4 hours and then at a speed of 10 miles per hour with 7-6 = 1 hour. Such driving distance of (2) (20) + (4) (30) + (1) (10) = 40 + 120 + 10 = 170 miles. Please note that the total elapsed time since the trip is always the beginning, rather than starting from the log on an entry.

Ideas analysis

We need only one element lb continuous recording a time b, then b-lb is this speed travel time, just input is ordered,

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
using namespace std;

int main() {
	int n, sum, a, b, lb;//lb记录上一个行程所用的时间
	while (cin >> n && n > -1) {
		sum = 0; lb = 0;
		for (int i = 0; i < n; i++) {
			cin >> a >> b;
			sum += (b - lb)*a;
			lb = b;
		}
		cout << sum << " miles" << endl;
	}
}
Published 186 original articles · won praise 13 · views 9295

Guess you like

Origin blog.csdn.net/csyifanZhang/article/details/105255537