【发布时间】:2018-03-27 13:17:07
【问题描述】:
我需要帮助编写一些基本的伪代码。
我有一个数据集,其中包含出租车行程的开始和结束时间(以分钟为单位)(end_time、start_time)。我需要帮助编写一些伪代码来计算有多少行程超过 1 小时。
durations 一次读取一个,并且只能在到达文件末尾之前读取。
【问题讨论】:
-
你能在你的文件中提供一个示例数据吗?
标签: algorithm pseudocode
我需要帮助编写一些基本的伪代码。
我有一个数据集,其中包含出租车行程的开始和结束时间(以分钟为单位)(end_time、start_time)。我需要帮助编写一些伪代码来计算有多少行程超过 1 小时。
durations 一次读取一个,并且只能在到达文件末尾之前读取。
【问题讨论】:
标签: algorithm pseudocode
让我们通过一个例子来尝试为上述问题编写一个算法。
假设有一个包含end_time 和start_time 的5 条目的文件,如下所示:-
1. 09:00 am - 10:05 am
2. 11:00 am - 11:15 am
3. 12:00 pm - 12:30 pm
4. 15:00 pm - 18:00 pm
5. 07:00 am - 07:10 am
由于帖子没有提供示例数据,我假设数据看起来类似于我上面写的,其中first time 是我们的start_time,second time 是我们的end_time。
现在我们的算法看起来像这样:-
Step 1. Load the file which has data. Also create a global variable to store count and initialize it to zero.
Step 2. Start Reading the file line by line and store each line in a temporary variable.
Step 3. Parse the line i.e split the line in the above example based on ' ' (space).
Step 4. Once we get the start and end time while parsing the line, calculate the time difference and store this difference in a variable called diff_time.
Step 5. if (diff_time > 1 hour) { count++; }
Step 6. Print the count.
希望这会有所帮助!
【讨论】: