首页 > 试题广场 >

时间转换

[编程题]时间转换
  • 热度指数:3134 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个12小时制的时间,请将其转换成24小时制的时间。说明:12小时制的午夜12:00:00AM,对应的24小时制时间为00:00:00。12小时制的中午12:00:00PM,对应的24小时制时间为12:00:00。

输入描述:
一个描述12小时制时间的字符串。所有的输入都是合理的,不用考虑输入不合理的情况。


输出描述:
一个描述24小时制时间的字符串。
示例1

输入

08:03:45PM

输出

20:03:45
/*
思路:十二小时制:AM中从0-11都是二十四小时制一样 12AM改成00:00:00
十二小时制的PM则需要开始变化,把每个时辰都加上12(时位)
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; 
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] str = br.readLine().split(":");
        //分情况讨论
        //①AM且小于12:00:00
        int hour = Integer.parseInt(str[0]);
        char ch = str[2].charAt(2);
        if(hour<12 && ch == 'A'){
            System.out.println(str[0] + ":" + str[1] + ":" + str[2].substring(0,2));
        }else if(hour == 12 && ch == 'A'){
            System.out.println("00" + ":" + str[1] + ":" + str[2].substring(0,2));
        }else if(hour <12 && ch == 'P'){
            hour = hour+12;
            System.out.println(String.valueOf(hour) + ":" + str[1] + ":" + str[2].substring(0,2));
        }else if(hour == 12 && ch == 'P'){
            System.out.println("12" + ":" + str[1] + ":" + str[2].substring(0,2));
        }
    }
}

发表于 2020-05-20 19:32:47 回复(0)
var a=readline();
if(a=='12:00:00PM'){
    print('12:00:00')
}else{
if(a=='12:00:00AM'){
    print('00:00:00')
}else{
    print(a[a.length-2]=='P' ? a.slice(0,a.length-2).replace(/\d+(?=:)/,a.slice(0,2)*1+12):a.slice(0,a.length-2))
}
}


发表于 2020-05-18 11:08:49 回复(0)
st=input()#特殊情况单独处理如果是下午就加12个小时
if st=='12:00:00AM' : print('00:00:00')
elif st=='12:00:00PM' : print('12:00:00')
elif st[-2:]=='PM' : print(str(int(st[:2])+12)+st[2:8])
else : print(st[:8])

发表于 2019-12-27 11:21:44 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main(){
    string s,t;
    cin>>s;
    int l = s.length();
    t = s.substr(0,l-2);
    if(s.substr(l-2,2)=="AM"){
        if(t=="12:00:00")
            cout<<"00:00:00"<<endl;
        else
            cout<<t<<endl;
    }else if(s.substr(l-2,2)=="PM"){
        if(t=="12:00:00")
            cout<<t<<endl;
        else{
            int h = stoi(t.substr(0,2));
            cout<<h+12<<t.substr(2,l-2)<<endl;
        }
    }

    return 0;
}

发表于 2019-10-30 01:09:47 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main(){
    string str;
    cin>>str;
    if(str.substr(8,2)=="AM"){
        if(str[0]=='1'&&str[1]=='2')
            cout<<"00:00:00";
        else
            cout<<str.substr(0,8);
    }
    else{
        if(str[0]=='1'&&str[1]=='2')
            cout<<str.substr(0,8);
        else{
            int h=stoi(str.substr(0,2));
            h+=12;
            cout<<h<<str.substr(2,6);
        }
    }
    return 0;
}

发表于 2019-10-15 16:50:28 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin>>s;
    int shi=0,fen=0,miao=0,f=0;
    string t=s.substr(s.size()-2),s1=s.substr(2,6),s2=s.substr(0,8);
    for(int i=0;i<2;i++)
        shi=shi*10+s[i]-'0';
    if(t=="AM")
    {
        if(shi==12)
            cout<<"00"<<s1<<endl;
        else
            cout<<s2<<endl;
    }
    else
    {
        if(shi==12)
            cout<<s2<<endl;
        else
        {
            if((shi+12)%24<10)
                cout<<"0";
            cout<<(shi+12)%24<<s1<<endl;
        }
    }
    return 0;
}

发表于 2019-08-21 20:26:24 回复(0)

除去两个特例,其他都符合常识

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        int hour = Integer.parseInt(str.substring(0, 2));
        String min_sec = str.substring(2, 8);
        if (str.equals("12:00:00AM")) {
            System.out.println("00:00:00");
        } else if (str.equals("12:00:00PM")) {
            System.out.println("12:00:00");
        } else if (str.charAt(8) == 'P') {
            System.out.println((hour + 12) + min_sec);
        } else {
            System.out.println(str.substring(0, 8));
        }
    }
}
发表于 2019-07-31 16:09:59 回复(0)
#include<cstdio>

int main(void) {
    int hour,min,s;
    char ss[3];
    scanf("%d:%d:%d%s",&hour,&min, &s, ss);
    if(*ss =='A') {
        if(hour==12) hour=0;
        printf("%02d:%02d:%02d",hour,min,s);
    }else {
        hour+=12;
        if(hour==24) hour  = 12;
        printf("%02d:%02d:%02d",hour,min,s);
        
    }
    return 0;
}

发表于 2021-04-10 19:20:34 回复(0)
//用了stoi函数,剩下的就是注意12:00:00AM和12:00:00PM两个时刻了。比较简单。
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s,temp;
	while (cin >> s)
	{
		temp = s.substr(0, 2);
		int i = stoi(temp, 0, 10);
		if (s[8] == 'A'&&s!="12:00:00AM")
		{
			cout << s.substr(0, 8) << endl;
		}
		else if(s[8]=='P'&&s!= "12:00:00PM")
		{
			i += 12;
			cout << i << s.substr(2, 6) << endl;
		}
		else if (s == "12:00:00AM")
		{
			cout << "00:00:00" << endl;
		}
		else if (s == "12:00:00PM")
		{
			cout << "12:00:00" << endl;
		}
	}
}

发表于 2020-07-22 01:04:38 回复(0)
//原来12:00:00AM是凌晨啊
import java.util.Scanner;
public class Main{
    public static String change(String time){
        Character c=time.charAt(time.length()-2);
        int hour=Integer.valueOf(time.substring(0,2));      
        if(c=='P'){
            if(hour!=12)           
           hour+=12;               
        }
        if(c=='A'&&hour==12)
            return "00"+time.substring(2,time.length()-2);
        if(hour>=10)
        return hour+time.substring(2,time.length()-2);
        else
        return "0"+hour+time.substring(2,time.length()-2);
    }
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in );
        String time=sc.nextLine();
        System.out.println(change(time));
    }
}

编辑于 2020-06-27 21:33:45 回复(0)
int(“05”)居然可以
发表于 2020-06-24 18:24:36 回复(0)
s = input()

if 'AM' in s:
    if "12:00:00" in s:
        print("00:00:00")
    else:
        print(s[:-2])

else:
    if "12:00:00" in s:
        print("12:00:00")
    else:
        l = s[:-2]
        a = int(l[:2]) + 12
        print(str(a) + l[2:])

发表于 2020-06-16 09:31:33 回复(0)
/*
这种写法是不是有点sha?
*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <limits.h>
#include <stack>
#include <unordered_map>
#include <map>
#include <queue>

using namespace std;

void TimeScaleConversion() {
    string s;
    cin >> s;
    int len = s.size();
    int h = 0;
    string h0, h1, res;
    if (s == "00:00:00AM" || s == "12:00:00AM")
    {
        res = "00:00:00";
    }
    else if(s == "12:00:00PM" || s == "00:00:00PM")
    {
        res = "12:00:00";
    }
    else if (s[8] == 'A')
    {
        h = 10 * (s[0] - '0') + (s[1] - '0');
        h1 = h % 10 + '0';
        h0 = h / 10 % 10 + '0';
        res = h0 + h1 + s.substr(2, 6);
    }
    else
    {
        h = 10 * (s[0] - '0') + (s[1] - '0') + 12;
        h1 = h % 10 + '0';
        h0 = h / 10 % 10 + '0';
        res = h0 + h1 + s.substr(2, 6);
    }
    cout << res << endl;
}

int main(){
    TimeScaleConversion();    return 0;
}

编辑于 2020-06-02 23:23:03 回复(0)
#include<iostream>
(720)#include<string>

using namespace std;

int main(void){
    int hour, min, sec;
    string str;
    char c;
    string shour, smin, ssec;
    cin>>hour>>c>>min>>c>>sec>>str;
    if (hour == 12 && min == 0 && sec == 0 && str == "AM"){
        cout<<"00:00:00"<<endl;
        return 0;
    }
    else if(hour == 12 && min == 0 && sec == 0 && str == "PM"){
        cout<<"12:00:00"<<endl;
        return 0;
    }
    if (str == "PM")
        shour = to_string(hour + 12);
    else if(hour < 10){
        shour = to_string(0) + to_string(hour);
    }
    else{
        shour = to_string(hour);
    }
    
    if (min < 10){
        smin = to_string(0) + to_string(min);
    }
    else
        smin = to_string(min);
    
    if (sec < 10){
        ssec = to_string(0) + to_string(sec);
    }
    else
        ssec = to_string(sec);
    cout<<shour<<":"<<smin<<":"<<ssec<<endl;
    return 0;
}

发表于 2020-05-09 17:14:35 回复(0)
s = input()
if s == '12:00:00AM':
    print('00:00:00')
elif s == '12:00:00PM':
    print('12:00:00')
elif s[:2] == '12' and s[-2:] == 'PM':
    print(s[:-2])
elif s[:2] == '12' and s[-2:] == 'AM':
    print('00'+s[2:-2])
else:
    if s[-2:] == 'AM':
        print(s[:-2])
    else:
        hour = int(s[:2])
        hour += 12
        print(str(hour)+s[2:-2])

发表于 2019-09-18 23:32:25 回复(0)
n = list(map(str,input().split()))
n = list(n[0])
hour = str(n[0])+str(n[1])
other = str(n[2])+str(n[3])+str(n[4])+str(n[5])+str(n[6])+str(n[7])
if n[-2] == 'A':
    if hour < '12':
        time = hour+other
        print(time)
    else:
        a = '00'
        time = a+other
        print(time)
else:
    hour = str(int(hour)+12)
    if hour > '23':
        hour = '12'
        time = hour+other
    else:
        time = hour+other
    print(time)

发表于 2019-09-16 13:38:16 回复(0)
c++
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string str;
    cin >> str;
    int len = str.size();
    if(str[len-2] == 'A')
    {
        if(str.substr(0,2) == "12") cout << "00"+str.substr(2,len-4) << endl;
        else cout << str.substr(0, len-2);
    }
    else{
        if(str.substr(0,2) == "12") cout << str.substr(0,len-2) <<endl;
        else cout << to_string(stoi(str.substr(0,2))+12) <<str.substr(2,len-4) <<endl;
    }
    return 0;
}

发表于 2019-08-05 10:10:26 回复(0)
oldstyle = input()
newstyle = ""

if oldstyle == "12:00:00AM":
    newstyle = "00:00:00"
elif oldstyle == "12:00:00PM":
    newstyle = "12:00:00"
elif oldstyle[-2:] == "AM":
    newstyle = oldstyle[:-2]
else:
    hour = int(oldstyle[:2])
    hour = 12 + hour
    newstyle = str(hour) + oldstyle[2:-2]

print(newstyle)

发表于 2019-08-04 14:44:16 回复(0)
简单的截取判断!

import java.util.*;
 public class Main {
     public static void main(String[] args){
        Scanner sc= new Scanner(System.in);
        String nums = sc.next();
        int hour=Integer.valueOf(nums.substring(0, 2));        
        String str=nums.substring(2, 8);    
        String flag=nums.substring(8, 10);
        if(hour==12 && flag.equals("AM"))
            System.out.print("00");    
        else 
            {
            if(flag.equals("PM")&& hour!=12)
            hour+=12;
            if(hour/10<1)
                System.out.print("0");    
            System.out.print(hour);        
            }            
        System.out.println(str);        
     }
发表于 2019-07-25 10:03:11 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        int i = Integer.parseInt(str.substring(0,2));
        if (str.contains("PM")) {
            if (i < 12) {
                str = i + 12 + str.substring(2, str.length() - 2);
            } else {
                str = str.substring(0, str.length() - 2);
            }
        }
        if (str.contains("AM")) {
            if (i < 12) {
                str = str.substring(0, str.length() - 2);
            } else {
                str = "00" + str.substring(2, str.length() - 2);
            }
        }
        System.out.println(str);
    }
}

发表于 2019-07-13 19:50:45 回复(0)