SRM 172 DIV1 Easy - BadClock (復習○)

問題


http://community.topcoder.com/stat?c=problem_statement&pm=1969&rd=4665

正確な時計と、1時間あたり必ずx分ズレる時計がある。
各時計は1~12時までの表示になる。

正確な時計とズレる時計の現在時刻が与えられた時、
時計の時刻が一緒になるまでに必要な時間を求める。

解き方


現在時刻のズレDとx分のずれの正負が異なれば答えはD/xになる。
一方で、正負が一緒であれば一周する必要があるので、(43200-D)/xとなる。

コード


using namespace std;

#define all(c) (c).begin(),(c).end()
#define FORE(i,d,e) for(int i=d;i<e;i++)
#define FOR(i,s,e) for (int i = int(s); i != int(e); i++)
#define FORIT(i,c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ISEQ(c) (c).begin(), (c).end()

class BadClock {

public:

int calc(string str){
int h=(str[0]-'0')*10+(str[1]-'0');
int m=(str[3]-'0')*10+(str[4]-'0');
int s=(str[6]-'0')*10+(str[7]-'0');
return h*3600+m*60+s;
}

double nextAgreement(string trueTime, string skewTime, int hourlyGain) {
int s1=calc(trueTime);
int s2=calc(skewTime);

if(s1==s2)return 0;

double dif=0;
if((s2-s1)*hourlyGain<0)dif=abs(s2-s1);
else dif=43200.0-abs(s2-s1);

return fabs(dif/hourlyGain);
}

};
このエントリーをはてなブックマークに追加