C++ 计算C+的军用时间差+;
我必须写一个程序,给出军事时间格式中给出的两个时间之间的差值。比如说, 请输入第一次:1730 请输入第二次:0900 输出=15小时30分钟 这是我提出的程序(*不允许使用if语句、循环或函数) 我只想知道这是对还是错C++ 计算C+的军用时间差+;,c++,C++,我必须写一个程序,给出军事时间格式中给出的两个时间之间的差值。比如说, 请输入第一次:1730 请输入第二次:0900 输出=15小时30分钟 这是我提出的程序(*不允许使用if语句、循环或函数) 我只想知道这是对还是错 int main() { int first; int second; cout << "Please enter the first time:"; cin >> first; cout << e
int main()
{
int first;
int second;
cout << "Please enter the first time:";
cin >> first;
cout << endl;
cout << "Please enter the second time:";
cin >> second;
cout << endl;
int am = first - 1200;
second = second + 1200;
int amhour = am/100;
int ammins = am%100;
int hours = second - amhour*100;
int real_hr = hours - 40;
int final_time = real_hr - ammins;
int final_hours = final_time/100;
int final_mins = final_time%100;
cout << final_hours << " hours and " << final_mins << " minutes." << endl;
return 0;
}
intmain()
{
int优先;
int秒;
cout>首先;
第二;
cout我首先将每次转换为“午夜后的分钟数”,然后从第二次中减去第一次,然后使用它来形成输出
但是,您可能需要考虑,如果第一次是2330
,第二次是0030
(跨越日边界),减法可能会给您一个负数
你可以通过简单地增加1440分钟(一天)来解决这个问题,如果超过一天,那么使用模运算来减少这个问题
因此,我将从以下内容开始:
#include <iostream>
int main() {
// Get times from user.
int first, second;
std::cout << "Please enter the first time: "; std::cin >> first;
std::cout << "Please enter the second time: "; std::cin >> second;
// Convert both to minutes since midnight.
first = (first / 100) * 60 + first % 100;
second = (second / 100) * 60 + second % 100;
// Work out time difference in minutes, taking into
// account possibility that second time may be earlier.
// In that case, it's treated as the following day.
const int MINS_PER_DAY = 1440;
int minutes = (second + MINS_PER_DAY - first) % MINS_PER_DAY;
// Turn minutes into hours and residual minutes, then output.
int hours = minutes / 60;
minutes = minutes % 60;
std::cout << hours << " hours and " << minutes << " minutes.\n";
}
#包括
int main(){
//从用户处获取时间。
int第一,第二;
std::cout>第一;
std::cout>second;
//将两者转换为午夜后的分钟数。
第一个=(第一个/100)*60+第一个%100;
秒=(秒/100)*60+秒%100;
//计算出以分钟为单位的时差,考虑到
//考虑到第二次可能更早的可能性。
//在这种情况下,它被视为第二天。
const int MINS_/天=1440;
整数分钟=(秒+分钟/天-第一次)%分钟/天;
//将分钟转换为小时和剩余分钟,然后输出。
整小时=分钟/60;
分钟=分钟%60;
std::它能为您尝试过的所有输入提供正确的输出吗?当然,您可以决定它是否正确。2200、2117不能给出正确的结果,所以您的答案是不,程序不正确。我不理解-40
的用途,也不理解转换为数学上可管理的时间的目的系统类似于am/pm
。提示:对于24小时时钟,表示为HHMM
的时间实际上是HH*60+MM
分钟(忽略夏令时)。转换为分钟,做数学运算,转换回小时/分钟。完成。我也讨厌这样的问题,因为它们是错误的。除非明确说明,否则无法知道从0030
到0400
之间经过了多少分钟和小时.不管怎样,谁会使用12小时的上午/下午时钟呢?不知道,这和以英里为单位测量距离一样有意义……;)