公式
B - Sensor Data Logging 解説 by en_translator
Implement just as instructed in the problem statement.
- The reading at time \(0\) is always saved. Saved it as the “memorized value,” and print the information for time \(0\).
- For \(i=1,2,\dots,T\), do the following:
- Obtain the absolute difference between the memorized value and the current reading.
- To find the absolute value, one can use the
absfunction in C++, for example. Even if such a function does not exist, one can use the formula \(|a-b| = \max(a,b)-\min(a,b)\) to obtain the absolute difference.
- To find the absolute value, one can use the
- Obtain the absolute difference between the memorized value and the current reading.
- If the difference between the two values is \(x\) or greater, store the current value as the new memorized value, and print the information for time \(i\).
The sequence of operation can be implemented by combining for and if statements.
Sample code (C++):
#include<bits/stdc++.h>
using namespace std;
int main(){
int t,x;
cin >> t >> x;
int save;
cin >> save;
cout << "0 " << save << "\n";
for(int i=1;i<=t;i++){
int a;
cin >> a;
if(abs(save-a)>=x){
save=a;
cout << i << " " << save << "\n";
}
}
return 0;
}
投稿日時:
最終更新: