HomeProjects
 
  

TC SRM144 DIV2 200pts C++

March 15, 2014

In this question we are given an integer, seconds. And the question asked us to return it in the hours:minutes:seconds format. The given integer, seconds, is between 0 and 86399.

I defined 3 integers: hour, min, sec. I assigned seconds%60 to sec in order to find the remaining from its devision by 60. That will give me the sec part of the format because everything that can be divisible by 60 will be consumed by minutes and hours. After that i did the same to min and hour. While i was calculating hour, i didn't need to take its module because the input was between 0 and 86399 and it could not pass the value of 24.

The code is here:

#include <iostream>
#include <string>
using namespace std;
class Time{
public:
string whatTime(int seconds)
{
int sec, hour, min;
sec=seconds%60;
min=(seconds/60)%60;
hour=seconds/3600;
return to_string(hour)+":"+to_string(min)+":"+to_string(sec);
}
};

CharacterX

 

 

© 2023 Built with Gatsby