Time Conversion

Problem Statement

You are given time in AM/PM format. Can you convert this into a 24-hour format? 

Input

Input consists of time in the AM/PM format i.e. hh:mm:ssAM or hh:mm:ssPM 
where 01hh12

Sample: 07:05:45PM

Output

You need to print the time in a 24-hour format i.e. hh:mm:ss 
where 00hh23

Sample output for the above input: 19:05:45

Note: Midnight is 12:00:00AM or 00:00:00. Noon is 12:00:00PM.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    string time="";
    cin>>time;
    
    if(time[time.size()-2]=='A'){
        if(time.substr(0,2)=="12") cout<<"00"<<time.substr(2,time.size()-4)<<endl;
        else cout<<time.substr(0,time.size()-2)<<endl;
    }
    
    if(time[time.size()-2]=='P'){
        if(time.substr(0,2)=="12") cout<<time.substr(0,time.size()-2)<<endl;
        else cout<<to_string(stoi(time.substr(0,2))+12)<<time.substr(2,time.size()-4)<<endl;
    }
    
    return 0;
}

 

posted @ 2015-11-08 09:37  飞飞喵  阅读(252)  评论(0编辑  收藏  举报