6번 오븐 시계

문제

KOI 전자에서는 건강에 좋고 맛있는 훈제오리구이 요리를 간편하게 만드는 인공지능 오븐을 개발하려고 한다. 인공지능 오븐을 사용하는 방법은 적당한 양의 오리 훈제 재료를 인공지능 오븐에 넣으면 된다. 그러면 인공지능 오븐은 오븐구이가 끝나는 시간을 분 단위로 자동적으로 계산한다. 또한, KOI 전자의 인공지능 오븐 앞면에는 사용자에게 훈제오리구이 요리가 끝나는 시각을 알려 주는 디지털시계가 있다. 훈제오리구이를 시작하는 시각과 오븐구이를 하는 데 필요한 시간이 분단위로 주어졌을 때, 오븐구이가 끝나는 시각을 계산하는 프로그램을 작성하시오.

 

입력
첫째 줄에는 현재 시각이 나온다. 현재 시각은 시 A (0 ≤ A ≤ 23)와 분 B (0 ≤ B ≤ 59)가 정수로 빈칸을 사이에 두고 순서대로 주어진다. 두 번째 줄에는 요리하는 데 필요한 시간 C (0 ≤ C ≤ 1,000)가 분 단위로 주어진다.

 

출력

첫째 줄에 종료되는 시각의 시와 분을 공백을 사이에 두고 출력한다. (단, 시는 0부터 23까지의 정수, 분은 0부터 59까지의 정수이다. 디지털시계는 23시 59분에서 1분이 지나면 0시 0분이 된다.)

 

훈제오리구이 맛있겠다.

입력은 두 번 들어오고 처음에 현재 시각, 두 번째 조리에 필요한 시간이다.

종료되는 시간은 현재 시각에서 조리 시간을 더한 결과이다. 주의할 점은 조리에 필요한 값은 분 단위로 들어온다는 것으로 이를 시간과 분으로 구분하는 게 계산하기 편할 것 같다.

 

C++

#include <iostream>
using namespace std;
int main(){
    int currH, currM, needM;
    cin >> currH;
    cin >> currM;
    cin >> needM;
    int endH, endM;
    int needH = needM / 60;
    needM = needM - needH * 60;
    endH = currH + needH;
    endM = currM + needM;
    if (endM >= 60){
        endM = endM - 60;
        endH++;
    }
    if (endH >= 24)
        endH = endH - 24;
    cout << endH << " " << endM;
    return 0;
}

 

C#

using System;
class Program{
    static void Main(string[] args){
        string input_first = Console.ReadLine();
        string input_second = Console.ReadLine();
        string[] arr_input_first = input_first.Split(' ');
        int currH = int.Parse(arr_input_first[0]);
        int currM = int.Parse(arr_input_first[1]);
        int needM = int.Parse(input_second);
        int endH, endM;
        int needH = needM / 60;
        needM = needM % 60;
        endH = currH + needH;
        endM = currM + needM;
        if (endM >= 60){
            endM = endM - 60;
            endH++;
        }
        if (endH >= 24)
            endH = endH - 24;
        Console.WriteLine($"{endH} {endM}");
    }
}

 

조리시간을 시간과 분으로 구분할 때 분은 몫으로 계산하면 간략하게 표현할 수 있다.

 

Python

inputFirst = input();
inputSecond = input();
arrInputFirst = inputFirst.split(' ');
currH = int(arrInputFirst[0]);
currM = int(arrInputFirst[1]);
needM = int(inputSecond);
needH = needM // 60;
needM = needM % 60;
endH = currH + needH;
endM = currM + needM;
if endM >= 60:
    endM -= 60
    endH += 1
if endH >= 24:
    endH -= 24
print(endH, endM)

 

파이썬 사용 시 정수 나눗셈에 유의한다. '//'

그리고 증감 연산자 '++' , '--'는 사용하지 못한다.

 

Node.js

const readline = require('readline');
const rl = readline.createInterface({
    input : process.stdin,
    output : process.stdout
});
rl.question('', (answer_first)=>{
    const [currH, currM] = answer_first.split(' ').map(Number);
    rl.question('', (answer_second)=>{
        let needM = parseInt(answer_second);
        let needH = Math.floor(needM / 60);
        needM = needM % 60;
        let endH = currH + needH;
        let endM = currM + needM;
        if (endM >= 60){
            endM -= 60;
            endH++;
        }
        if (endH >= 24){
            endH -= 24;
        }
        console.log(`${endH} ${endM}`);
        rl.close();
    });
});

 

endH의 초기값을 구할 때 Math.floor를 사용해서 정수값을 구한다.

 

728x90
반응형

5번 알람 시계

문제

상근이는 매일 아침 알람을 듣고 일어난다. 알람을 듣고 바로 일어나면 다행이겠지만, 항상 조금만 더 자려는 마음 때문에 매일 학교를 지각하고 있다. 상근이는 모든 방법을 동원해 보았지만, 조금만 더 자려는 마음은 그 어떤 것도 없앨 수가 없었다. 이런 상근이를 불쌍하게 보던 창영이는 자신이 사용하는 방법을 추천해 주었다. 바로 "45분 일찍 알람 설정하기"이다. 이 방법은 단순하다. 원래 설정되어 있는 알람을 45분 앞서는 시간으로 바꾸는 것이다. 어차피 알람 소리를 들으면, 알람을 끄고 조금 더 잘 것이기 때문이다. 이 방법을 사용하면, 매일 아침 더 잤다는 기분을 느낄 수 있고, 학교도 지각하지 않게 된다. 현재 상근이가 설정한 알람 시각이 주어졌을 때, 창영이의 방법을 사용한다면, 이를 언제로 고쳐야 하는지 구하는 프로그램을 작성하시오.

 

입력

첫째 줄에 두 정수 H와 M이 주어진다. (0 ≤ H ≤ 23, 0 ≤ M ≤ 59) 그리고 이것은 현재 상근이가 설정한 알람 시간 H시 M분을 의미한다. 입력 시간은 24시간 표현을 사용한다. 24시간 표현에서 하루의 시작은 0:0(자정)이고, 끝은 23:59(다음날 자정 1분 전)이다. 시간을 나타낼 때, 불필요한 0은 사용하지 않는다.

 

출력

첫째 줄에 상근이가 창영이의 방법을 사용할 때, 설정해야 하는 알람 시간을 출력한다. (입력과 같은 형태로 출력하면 된다.)

 

상근이의 아침은 나의 아침과 비슷해 보인다. 문제는 시간과 분이 입력될 때 45분 전의 시간을 출력하면 된다.

24시간 표현을 사용하고, 하루의 시작은 0:0 끝은 23:59인 점을 유의한다. 그리고 표기 시 불필요한 0은 생략

 

C++

#include <iostream>
using namespace std;
int main(){
    int input_h, input_m;
    cin >> input_h;
    cin >> input_m;
    int m = input_m - 45;
    int h = input_h;
    if (m < 0){
        h = input_h - 1;
        m = 60 + m;
        if (h < 0)
        {
            h = 24 + h;
        }
    }
    cout << h << " " << m;
    return 0;
}

 

C#

using System;
class Program{
    static void Main(string[] args){
        string input = Console.ReadLine();
        string[] arr = input.Split(' ');
        int input_h = int.Parse(arr[0]);
        int input_m = int.Parse(arr[1]);
        int m = input_m - 45;
        int h = input_h;
        if (m < 0){
            h = input_h - 1;
            m = 60 + m;
            if (h < 0) {
                h = 24 + h;
            }
        }
        Console.WriteLine($"{h} {m}");
    }
}

 

Python

inputData = input();
inputArr = inputData.split(' ');
inputH = int(inputArr[0]);
inputM = int(inputArr[1]);
h = inputH;
m = inputM - 45;
if m < 0:
    h = inputH - 1;
    m = 60 + m;
    if h < 0:
        h = 24 + h;
print(f"{h} {m}");

 

파이썬에서 문자열에 변수를 사용하는 법

 

1. 문자열 변환

print(str(h) + " " + str(m))

 

2. f-string 

파이썬 3.6 이상부터

print(f"{h} {m}")

 

3. format

print 함수는 여러 인수를 받을 경우 공백으로 구분해서 문자열을 출력한다.

print(h, m)

 

Node.js

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split(' ');
const inputH = parseInt(input[0]);
const inputM = parseInt(input[1]);
let h = inputH;
let m = inputM - 45;
if (m < 0){
    m = 60 + m;
    h = h - 1;
    if (h < 0){
        h = 24 + h;
    }
}
console.log(`${h} ${m}`);

 

자바스크립트에서 리터럴 템플릿을 사용할 때는 백틱을 사용해야 하며 각각의 중괄호 앞에 $ 표기를 해야 한다.

728x90
반응형

3번 사분면 고르기

문제

흔한 수학 문제 중 하나는 주어진 점이 어느 사분면에 속하는지 알아내는 것이다. 사분면은 아래 그림처럼 1부터 4까지 번호를 갖는다. "Quadrant n"은 "제 n사분면"이라는 뜻이다.

https://www.acmicpc.net/problem/14681

 

예를 들어, 좌표가 (12, 5)인 점 A는 x좌표와 y좌표가 모두 양수이므로 제1사분면에 속한다. 점 B는 x좌표가 음수이고 y좌표가 양수이므로 제2사분면에 속한다. 점의 좌표를 입력받아 그 점이 어느 사분면에 속하는지 알아내는 프로그램을 작성하시오. 단, x좌표와 y좌표는 모두 양수나 음수라고 가정한다.

 

입력

첫 줄에는 정수 x가 주어진다. (−1000 ≤ x ≤ 1000; x ≠ 0) 다음 줄에는 정수 y가 주어진다. (−1000 ≤ y ≤ 1000; y ≠ 0)

 

출력

점 (x, y)의 사분면 번호(1, 2, 3, 4 중 하나)를 출력한다.

 

x 가 양수인지 음수인지, y가 양수인지 음수인지 구분해서 조건

 

C++

#include <iostream>
using namespace std;
int main(){
    int x, y;
    cin >> x;
    cin >> y;
    char result;
    if (x > 0)
    {
        if (y > 0)
            result = '1';
        else 
            result = '4';
    }
    else{
        if (y > 0)
            result = '2';
        else
            result = '3';
    }
    cout << result;
    return 0;
}

 

 

C#

using System;
class Program{
    static void Main(string[] args){
        string inputX = Console.ReadLine();
        string inputY = Console.ReadLine();
        int x = int.Parse(inputX);
        int y = int.Parse(inputY);
        char result;
        if (x > 0)
        {
            if (y > 0)
                result = '1';
            else 
                result = '4';
        }
        else{
            if (y > 0)
                result = '2';
            else
                result = '3';
        }
        Console.WriteLine(result);
    }
}

 

앞의 문제들과 다르게 각 줄에 값들이 들어온다. 따라서 입력을 따로 두 번 받아서 값을 저장한다.

 

Python

x = int(input());
y = int(input());
if x > 0:
    if y > 0:
        result = '1';
    else:
        result = '4';
else:
    if y > 0:
        result = '2';
    else:
        result = '3';
print(result);

 

파이썬에서 중첩 조건문을 쓸 때는 들여 쓰기로 구분한다.

 

Node.js

const readline = require('readline');
const rl = readline.createInterface({
    input : process.stdin,
    output : process.stdout
});
let x, y;
let result;
rl.question('', (answerX)=>{
    x = parseInt(answerX, 10);
    rl.question('', (answerY)=>{
    y = parseInt(answerY, 10);
        if (x > 0){
            if (y > 0)
                result = '1';
            else
                result = '4';
        }
        else{
            if (y > 0)
                result = '2';
            else
                result = '3';   
        }
        console.log(result);
        rl.close();
    });
});

 

각 각의 입력을 받고 처리한다.

rl.close()가 호출되면 입력된 값을 사용할 수 없기 때문에 호출 전에 결과를 출력해야 한다.

728x90
반응형

+ Recent posts