码迷,mamicode.com
首页 > 其他好文 > 详细

Time Conversion

时间:2015-11-08 14:28:38      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

 

Time Conversion

标签:

原文地址:http://www.cnblogs.com/XingyingLiu/p/4946879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!