We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
std::stringnumToWords(intconst&_num){usingnamespacestd;autod1=_num/10;autod2=_num%10;autotime=string();switch(d1){case0:{break;}case1:{switch(d2){case0:{return"ten";}case1:{return"eleven";}case2:{return"twelve";}case3:{return"thirteen";}case4:{return"fourteen";}case5:{return"fifteen";}case6:{return"sixteen";}case7:{return"seventeen";}case8:{return"eighteen";}case9:{return"nineteen";}}}case2:{time+="twenty";break;}case3:{time+="thirty";break;}case4:{time+="fourty";break;}case5:{time+="fifty";break;}case6:{time+="sixty";break;}case7:{time+="seventy";break;}case8:{time+="eighty";break;}case9:{time+="ninety";break;}default:{time+="unknown d1!";}}if(d1!=0){time+=" ";}switch(d2){case0:{break;}case1:{returntime+"one";}case2:{returntime+"two";}case3:{returntime+"three";}case4:{returntime+"four";}case5:{returntime+"five";}case6:{returntime+"six";}case7:{returntime+"seven";}case8:{returntime+"eight";}case9:{returntime+"nine";}default:{returntime+="unknown d2!";}}return"unknown num!";}std::stringtimeInWords(intconst&_h,intconst&_m){usingnamespacestd;autotime=string();if(_m==0){return::numToWords(_h)+" o' clock";}elseif((1<=_m&&_m<=14)||(16<=_m&&_m<=29)){return::numToWords(_m)+(_m==1?" minute":" minutes")+" past "+::numToWords(_h);}elseif(_m==15){return"quarter past "+::numToWords(_h);}elseif(_m==30){return"half past "+::numToWords(_h);}elseif(_m==45){return"quarter to "+::numToWords((_h+1)%13);}else{return::numToWords(60-_m)+" minutes to "+::numToWords((_h+1)%13);}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Time in Words
You are viewing a single comment's thread. Return to all comments →
C++ (more at https://github.com/IhorVodko/Hackerrank_solutions , feel free to give a star :) )