C++ 为什么';t我的上午/下午时间到24小时,以C++;工作(hackerrank prob)
我一直在工作,从上午/下午到24小时,不知什么原因,我被卡住了。我不确定我在做什么假设/错误。我已经尝试了许多测试用例,但似乎无法创建错误的代码,因为它被拒绝了 这是一个好主意。我知道有解决方案,但我只想知道为什么我的代码不起作用,因为我想知道我做错了什么C++ 为什么';t我的上午/下午时间到24小时,以C++;工作(hackerrank prob),c++,C++,我一直在工作,从上午/下午到24小时,不知什么原因,我被卡住了。我不确定我在做什么假设/错误。我已经尝试了许多测试用例,但似乎无法创建错误的代码,因为它被拒绝了 这是一个好主意。我知道有解决方案,但我只想知道为什么我的代码不起作用,因为我想知道我做错了什么 #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm&g
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main(){
string time;
cin >> time;
string str_hours = time.substr(0,2);
string str_minutes = time.substr(3,2);
string str_seconds = time.substr(6,2);
string period = time.substr(8,2);
int hours = stoi(str_hours);
if(period.compare("PM") == 0) {
hours += 12;
}
str_hours = to_string(hours);
if(hours < 10) {
str_hours.insert(0,"0");
}
cout << str_hours << ":" << str_minutes << ":" << str_seconds;
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
串时间;
cin>>时间;
string str_hours=time.substr(0,2);
string str_minutes=time.substr(3,2);
string str_seconds=time.substr(6,2);
字符串周期=time.substr(8,2);
整小时=stoi(短小时);
如果(周期比较(“PM”)==0){
小时数+=12;
}
str_小时=到_字符串(小时);
如果(小时<10){
插入(0,“0”);
}
cout您的代码的问题在于它对边界条件的处理:它将中午15点的12:15:00PM
转换为无效时间24:15:00
同样的情况也发生在午夜12点,转换后即为中午12点。代码的问题在于其边界条件的处理:它将中午15点的12:15:00PM
转换为无效时间24:15:00
同样的情况也发生在凌晨12点,午夜12点,转换后的中午12点。你尝试了什么?你的具体问题是什么?你的调试器告诉你什么?你研究了什么?你不会只是被填鸭式地灌输一个解决方案。@JesperJuhl OP对他所做的尝试非常清楚。Hacker Rank不让他知道他的专业是什么问题,虽然,这是一个通过或失败的网站。你尝试了什么?你的具体问题是什么?你的调试器告诉你什么?你研究了什么?你不会只是被填鸭式地灌输一个解决方案,所以。@JesperJuhl OP对他所做的尝试非常清楚。黑客等级不让他知道他的问题是什么,尽管,这是一个通过或失败的网站。那非常感谢你!我知道我错过了一件非常简单的事情。非常感谢你!我知道我错过了一件非常简单的事情。