n = int(input())for _ in range(n): parts = input().split() hh, mm = parts[0].split(":") h = int(hh) if len(parts) == 1: # 24시간 → 12시간: 나머지가 0인 0시·12시는 12로 쓴다 h12 = 12 if h % 12 == 0 else h % 12 ampm = "AM" if h < 12 else "PM" print(f"{h12}:{mm} {ampm}") else: # 12시간 → 24시간: 12시를 먼저 0으로 바꾼 뒤 PM이면 12를 더한다 h24 = h % 12 + (12 if parts[1] == "PM" else 0) print(f"{h24:02d}:{mm}")
JavaScriptmain.js · 19 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const out = [];for (let i = 1; i <= n; i++) { const parts = lines[i].trim().split(" "); const [hh, mm] = parts[0].split(":"); const h = Number(hh); if (parts.length === 1) { // 24시간 → 12시간: 나머지가 0인 0시·12시는 12로 쓴다 const h12 = h % 12 === 0 ? 12 : h % 12; const ampm = h < 12 ? "AM" : "PM"; out.push(`${h12}:${mm} ${ampm}`); } else { // 12시간 → 24시간: 12시를 먼저 0으로 바꾼 뒤 PM이면 12를 더한다 const h24 = (h % 12) + (parts[1] === "PM" ? 12 : 0); out.push(`${String(h24).padStart(2, "0")}:${mm}`); }}console.log(out.join("\n"));
TypeScriptmain.ts · 20 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const out: string[] = [];for (let i = 1; i <= n; i++) { const parts: string[] = lines[i].trim().split(" "); const [hh, mm] = parts[0].split(":"); const h: number = Number(hh); if (parts.length === 1) { // 24시간 → 12시간: 나머지가 0인 0시·12시는 12로 쓴다 const h12: number = h % 12 === 0 ? 12 : h % 12; const ampm: string = h < 12 ? "AM" : "PM"; out.push(`${h12}:${mm} ${ampm}`); } else { // 12시간 → 24시간: 12시를 먼저 0으로 바꾼 뒤 PM이면 12를 더한다 const h24: number = (h % 12) + (parts[1] === "PM" ? 12 : 0); out.push(`${String(h24).padStart(2, "0")}:${mm}`); }}console.log(out.join("\n"));
JavaMain.java · 28 行
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)); int n = Integer.parseInt(br.readLine().trim()); StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { String[] parts = br.readLine().trim().split(" "); String[] hm = parts[0].split(":"); int h = Integer.parseInt(hm[0]); String mm = hm[1]; if (parts.length == 1) { // 24시간 → 12시간: 나머지가 0인 0시·12시는 12로 쓴다 int h12 = h % 12 == 0 ? 12 : h % 12; String ampm = h < 12 ? "AM" : "PM"; out.append(h12).append(':').append(mm).append(' ').append(ampm).append('\n'); } else { // 12시간 → 24시간: 12시를 먼저 0으로 바꾼 뒤 PM이면 12를 더한다 int h24 = h % 12 + (parts[1].equals("PM") ? 12 : 0); out.append(String.format("%02d", h24)).append(':').append(mm).append('\n'); } } System.out.print(out); }}
C#Main.cs · 32 行
using System;using System.Text;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()); var sb = new StringBuilder(); for (int i = 0; i < n; i++) { string[] parts = Console.ReadLine().Trim().Split(' '); string[] hm = parts[0].Split(':'); int h = int.Parse(hm[0]); string mm = hm[1]; if (parts.Length == 1) { // 24시간 → 12시간: 나머지가 0인 0시·12시는 12로 쓴다 int h12 = h % 12 == 0 ? 12 : h % 12; string ampm = h < 12 ? "AM" : "PM"; sb.Append($"{h12}:{mm} {ampm}\n"); } else { // 12시간 → 24시간: 12시를 먼저 0으로 바꾼 뒤 PM이면 12를 더한다 int h24 = h % 12 + (parts[1] == "PM" ? 12 : 0); sb.Append($"{h24:00}:{mm}\n"); } } Console.Write(sb); }}
C++main.cpp · 29 行
#include <iomanip>#include <iostream>#include <sstream>#include <string>int main() { std::string line; std::getline(std::cin, line); int n = std::stoi(line); for (int i = 0; i < n; i++) { // 12시간 표기 줄은 "7:05 PM"처럼 두 낱말이라 한 줄을 통째로 읽는다 std::getline(std::cin, line); std::istringstream in(line); std::string time, ampm; in >> time >> ampm; std::size_t colon = time.find(':'); int h = std::stoi(time.substr(0, colon)); std::string mm = time.substr(colon + 1); if (ampm.empty()) { // 24시간 → 12시간: 나머지가 0인 0시·12시는 12로 쓴다 int h12 = h % 12 == 0 ? 12 : h % 12; std::cout << h12 << ':' << mm << ' ' << (h < 12 ? "AM" : "PM") << '\n'; } else { // 12시간 → 24시간: 12시를 먼저 0으로 바꾼 뒤 PM이면 12를 더한다 int h24 = h % 12 + (ampm == "PM" ? 12 : 0); std::cout << std::setw(2) << std::setfill('0') << h24 << ':' << mm << '\n'; } }}
Cmain.c · 26 行
#include <stdio.h>#include <string.h>int main(void) { char line[64]; int n; if (fgets(line, sizeof line, stdin) == NULL) return 0; sscanf(line, "%d", &n); for (int i = 0; i < n; i++) { /* 12시간 표기 줄은 "7:05 PM"처럼 두 낱말이라 한 줄을 통째로 읽는다 */ fgets(line, sizeof line, stdin); int h; char mm[3], ampm[3]; int got = sscanf(line, "%d:%2s %2s", &h, mm, ampm); if (got == 2) { /* 24시간 → 12시간: 나머지가 0인 0시·12시는 12로 쓴다 */ int h12 = h % 12 == 0 ? 12 : h % 12; printf("%d:%s %s\n", h12, mm, h < 12 ? "AM" : "PM"); } else { /* 12시간 → 24시간: 12시를 먼저 0으로 바꾼 뒤 PM이면 12를 더한다 */ int h24 = h % 12 + (strcmp(ampm, "PM") == 0 ? 12 : 0); printf("%02d:%s\n", h24, mm); } } return 0;}
Craykonmain.cray · 21 行
n = コンソール.行読み().数に();繰り返す (i = 0; i < n; i++){ 칸 = コンソール.行読み().空白除去().分ける(" "); 시분 = 칸[0].分ける(":"); h = 시분[0].数に(); mm = 시분[1]; もし (칸.個数 == 1) { // 24시간 → 12시간: 나머지가 0인 0시·12시는 12로 쓴다 h12 = h % 12 == 0 ? 12 : h % 12; ampm = h < 12 ? "AM" : "PM"; コンソール.書く(h12 + ":" + mm + " " + ampm); } でなければ { // 12시간 → 24시간: 12시를 먼저 0으로 바꾼 뒤 PM이면 12를 더한다 h24 = h % 12 + (칸[1] == "PM" ? 12 : 0); コンソール.書く(("" + h24).前埋め(2, "0") + ":" + mm); }}