問題
http://community.topcoder.com/stat?c=problem_statement&pm=11329&rd=14540
解き方
30度ずつ回転させることが可能なので、全探索して
有効な数字の時の値を更新すればよい。
コード
class RotatedClock {
public: string getEarliest(int hourHand, int minuteHand) {
string ret="";
for(int i=0;i<=360;i+=30){
int h=(hourHand+i)%360;
int m=(minuteHand+i)%360;
if((h%30)*12==m){
char ch[20];
sprintf(ch,"%02d:%02d",h/30,m/6);
if(ret.empty()||ch<ret)ret=ch;
}
}
return ret;
}
};
public: string getEarliest(int hourHand, int minuteHand) {
string ret="";
for(int i=0;i<=360;i+=30){
int h=(hourHand+i)%360;
int m=(minuteHand+i)%360;
if((h%30)*12==m){
char ch[20];
sprintf(ch,"%02d:%02d",h/30,m/6);
if(ret.empty()||ch<ret)ret=ch;
}
}
return ret;
}
};