给你一个码农的上床时间和起床时间,计算他总共睡了多少小时?
#include<algorithm> #include<iostream> #include<string> #include<vector> using namespace std; int main() { string s1,s2; while(cin>>s1>>s2) { int h1 = (s1[0]-‘0‘)*10+(s1[1]-‘0‘); int h2 = (s2[0]-‘0‘)*10+(s2[1]-‘0‘); int m1 = (s1[3]-‘0‘)*10+(s1[4]-‘0‘); int m2 = (s2[3]-‘0‘)*10+(s2[4]-‘0‘); int flag =0;//有没有向小时借位 int sum=0; if(m1>m2) { flag = 1; sum += (m2 +60 -m1); } else { sum += (m2 -m1); } if(h2>=(h1+flag))//等于号不要忘了哦,临界判断要谨慎 { sum += ((h2-h1-flag)*60); } else { sum += ((h2+24-h1-flag)*60); } cout<<sum<<endl; } }
[ACM 1280]码农总共睡了多少小时?,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/Xylophone/p/3822481.html