Subscribed unsubscribe Subscribe Subscribe

yukicoder No.70 睡眠の重要性!

Problem

No.70 睡眠の重要性! - yukicoder

Solution

分に直して計算する。もし起きる時間の方が寝る時間より小さかったら+24時間する(分に直すには24*60=1440

Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(reader.readLine());

        int sum = 0;
        for (int i = 0; i < N; i++) {
            String[] inputs = reader.readLine().split(" ");
            String[] sleepTime = inputs[0].split(":");
            String[] wakeUpTime = inputs[1].split(":");
            int H1 = Integer.parseInt(sleepTime[0]);
            int M1 = Integer.parseInt(sleepTime[1]);
            int h1 = Integer.parseInt(wakeUpTime[0]);
            int m1 = Integer.parseInt(wakeUpTime[1]);
            int a = H1 * 60 + M1;
            int b = h1 * 60 + m1;
            if (a > b) {
                sum +=b - a + 1440;
            } else {
                sum += b-a;
            }

        }
        System.out.println(sum);
    }


}
Remove all ads