Skip to content

Instantly share code, notes, and snippets.

@shnya
Created January 12, 2011 14:52
Show Gist options
  • Save shnya/776241 to your computer and use it in GitHub Desktop.
Save shnya/776241 to your computer and use it in GitHub Desktop.
SRM 290 Div2 500
class BrokenClock
{
vector<string> split(const string &str, const string &delim){
vector<string> res;
size_t current = 0, found, delimlen = delim.size();
while((found = str.find(delim, current)) != string::npos){
res.push_back(string(str, current, found - current));
current = found + delimlen;
}
res.push_back(string(str, current, str.size() - current));
return res;
}
int atoi2(const string &str){
//return strtol(str.c_str(), NULL, 0);
//基数に0を指定すると、
//09などの文字列が来た時に8進数と認識してしまいバグったので基数10に修正。
//馬鹿だねぇ。
return strtol(str.c_str(), NULL, 10);
}
public:
int fewestClicks(string clockTime, string currentTime)
{
vector<string> v1 = split(clockTime, ":");
vector<string> v2 = split(currentTime, ":");
int clockHour = atoi2(v1[0]);
int clockMin = atoi2(v1[1]);
int curHour = atoi2(v2[0]);
int curMin = atoi2(v2[1]);
int hdiff = curHour - clockHour;
if(hdiff < 0) hdiff = curHour + (24 - clockHour);
clockMin += hdiff;
if(clockMin >= 60) clockMin -= 60;
int mdiff = curMin - clockMin;
if(mdiff < 0) mdiff = curMin + (60 - clockMin);
return hdiff + mdiff;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment