Topic
Describe:
给定两个合法的时间(格式固定:hh:mm:ss,时间合法,不用考虑其它情况),输入两个时间相加后的结果;注意,相加后的结果也必需是一个合法的时间;附合法时间定义:小时在[00-23]之间,分钟和秒分别是在[00-59]之间;
Run time limit:
无限制
Memory Limit:
无限制
Input:
时分秒格式的时间字符串,如00:00:00
Output:
时分秒格式的时间字符串,如00:00:00
Sample input:
00:00:00 00:00:01
Sample output:
00:00:01
Answer hint: It is recommended to convert time to seconds calculation
Code
/* ---------------------------------------* Date: 2015-07-06* sjf0115* title: Achieve two legal time add * Source: Huawei Machine Test real problem------------------- ----------------------*/#include <iostream>#include <string>#include <vector>#include <stack>#include <algorithm>using namespace Std;//int converted to stringString Int2str (intnum) {stringStr="";if(num = =0){Str="0";return Str; }//if while(num) {Str. Insert (Str. Begin (), num%Ten+' 0 '); Num/=Ten; }//while return Str;}//Two total legal time addedString Timeadd (String t1,string T2) {intH1 = Atoi (T1.substr (0,2). C_STR ());intH2 = Atoi (T2.substr (0,2). C_STR ());intM1 = Atoi (T1.substr (3,2). C_STR ());intM2 = atoi (T2.substr (3,2). C_STR ());intS1 = atoi (T1.substr (6,2). C_STR ());intS2 = atoi (T2.substr (6,2). C_STR ());ints = (s1 + s2)% -;intM = (M1 + m2 + (S1 + s2)/ -) % -;intH = (H1 + H2 + (M1 + m2)/ -) % -; StringStr="";if(H <Ten){Str+="0"; }//if Str+ = Int2str (h) +":";if(M <Ten){Str+="0"; }//if Str+ = Int2str (m) +":";if(S <Ten){Str+="0"; }//if Str+ = INT2STR (s);return Str;}//Method 2String TimeAdd2 (String t1,string T2) {intH1 = Atoi (T1.substr (0,2). C_STR ());intH2 = Atoi (T2.substr (0,2). C_STR ());intM1 = Atoi (T1.substr (3,2). C_STR ());intM2 = atoi (T2.substr (3,2). C_STR ());intS1 = atoi (T1.substr (6,2). C_STR ());intS2 = atoi (T2.substr (6,2). C_STR ());intTotal = H1 * -* -+ M1 * -+ S1 + H2 * -* -+ m2 * -+ s2;ints = Total% -;intm = Total/ -% -;inth = Total/ -/ -% -; StringStr="";if(H <Ten){Str+="0"; }//if Str+ = Int2str (h) +":";if(M <Ten){Str+="0"; }//if Str+ = Int2str (m) +":";if(S <Ten){Str+="0"; }//if Str+ = INT2STR (s);return Str;}intMain () {string t1,t2;//freopen ("c:\\users\\administrator\\desktop\\acm.in", "R", stdin); while(CIN>>T1>>T2) {Cout<<timeadd (T1,T2) <<" "<<TIMEADD2 (T1,T2) <<endl; }//while return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Huawei Machine test real problem] [2014]64. Implementing two legal time additions