当我在TopCoder上提交代码时,它只给了我200分SRM的60分 我认为这个算法有问题。
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
class Time
{
public:
string whatTime(int seconds)
{
int hours=0, minutes=0, secs=0;
ostringstream cHours, cMinutes, cSeconds;
string sHours, sMinutes, sSeconds;
string theReturns;
while (seconds > 0)
{
if (seconds > 3600)
{
hours++;
seconds -= 3600;
}
else if (seconds > 60)
{
minutes++;
seconds -= 60;
} else {
secs ++;
seconds--;
}
}
cHours << hours;
cMinutes << minutes;
cSeconds << secs;
sHours = cHours.str() + ":";
sMinutes = cMinutes.str() + ":";
sSeconds = cSeconds.str();
theReturns = sHours + sMinutes + sSeconds;
return (theReturns);
}
};
/* Main Function for Me, I didn't submitted it on TopCoder, they only want the class */
int main()
{
Time myTime;
cout << myTime.whatTime(3661);
return (0);
}
答案 0 :(得分:1)
您可能希望将ostringstream
的数量减少为一个。在一个流上使用流插入运算符。
此外,使用数学语句而不是“if”语句:
hours = seconds / 3600;
seconds_remaining = seconds % 3600;
数学语句将消除循环的需要。