列表

详情


NC225362. Digital Speedometer

描述

    A digital speedometer shows a vehicle’s speed as integer miles per hour. There are occasions when the sensed speed varies between two integer values, such as during cruise control. Using a single threshold to round between adjacent integers often makes the display toggle rapidly between the two integers, which is distracting to the driver.
    Your team must implement a smoothing technique for the display using separate rising and falling thresholds ( and  , , respectively). See Figure  for a graphical depiction of the Sample Input for use with the following rules. 
    Each sensed speed, , falls between two adjacent integers  and , , where . When displaying the sensed speed as an integer: 
  • When  falls between  and , is displayed as .
  • When falls between  and , is displayed as .
  • When falls between and , is displayed as if the most recent preceding value for outside of range  is less than , and is displayed as if the most recent preceding value for outside of range [i + tf , i + tr] is greater than
  •  Any sensed speed, , must display as  because any non-zero speed, no matter how small, must display as non-zero to indicate that the vehicle is in motion. 

输入描述

The first line of input contains , the falling threshold. The second line of input contains , the rising threshold. Thespeed sensor reports in increments of  mph. The thresholds are always set halfway between speed increments.All remaining lines until end-of-file are successive decimal speeds, , in miles per hour, one speed per line. The thirdline of input, which is the first measured speed, will always be . There are at most  observed speeds in input. 

输出描述

Output is the list of speeds, one speed per line, smoothed to integer values appropriate to  and .

示例1

输入:

0.25
0.75
0
2.0
5.7
5.8
5.7
5.2
5.7
0.8
0.2

输出:

0
2
5
6
6
5
5
1
1

说明:

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++ 11.0.1) 解法, 执行用时: 6ms, 内存消耗: 416K, 提交时间: 2023-07-11 19:10:08

#include <bits/stdc++.h>
using namespace std;
double tf, tr, s, ans;
int main()
{
	cin >> tf >> tr;
	while(cin>>s)
	{
		int i = s, j = i+1;
		if(s == 0)
			ans = 0;
		else if(s < 1)
			ans = 1;
		else if(i <= s && s < i+tf)
			ans = i;
		else if(i+tr < s && s <= j)
			ans = j;
		else if(ans < i+tr)
			ans = i;
		else
			ans = j;
		cout << ans << endl;
	}
}

上一题