본문 바로가기
Softeer 풀이

[Sofreer 문제풀이] Lv.1(주행거리 비교하기, 근무시간 A+B) feat. Java

by happyhelen 2023. 10. 25.

1. 주행거리 비교하기

import java.util.*;
import java.io.*;


public class Main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        int B = sc.nextInt();
        if(A>B){
            System.out.println("A");
        }else if(A<B){
            System.out.println("B");
        }else if(A==B){
            System.out.println("same");
        }
    }
}
  • Scanner 의 메소드
    • next() : 공백 한 칸을 구분으로 단어를 받아온다.
    • next + {자료형}() : 자료형에 맞는 값을 받을 수 있다. (nextInt(), nextDouble() 등,,)
    • nextLine() : 한 줄을 통째로 받아온다.

참고 url : https://ssungkang.tistory.com/entry/java%EC%82%AC%EC%9A%A9%EC%9E%90%EB%A1%9C-%EB%B6%80%ED%84%B0-%EA%B0%92-%EC%9E%85%EB%A0%A5%EB%B0%9B%EA%B8%B0-Scanner

 

 

 

2. 근무 시간

import java.util.*;
import java.io.*;


public class Main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        String[] arrStart = null;
        String[] arrEnd = null;
        int total = 0;

        while(sc.hasNext()){
            String start = sc.next();
            String end = sc.next();
            arrStart = start.split(":");
            arrEnd = end.split(":");

            int startTime = Integer.parseInt(arrStart[0]);
            int endTime = Integer.parseInt(arrEnd[0]);

            int startMin = Integer.parseInt(arrStart[1]);
            int endMin = Integer.parseInt(arrEnd[1]);

            if(startMin <= endMin) {
                total += (endTime - startTime)*60;
                total += endMin - startMin;
            }else{
                total += (endTime - startTime -1)*60;
                total += 60 - startMin + endMin;
            }
            
            // 위 연산을 아래로 요약할 수 있음
            // total += (60-startMin) + (endTime-startTime-1)*60 + endMin ;
            
            
        }
        System.out.println(total);
    }
}

 

 

 

2. A+B

import java.util.*;
import java.io.*;


public class Main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int caseCnt = sc.nextInt();

        for(int i=1; i<caseCnt +1; i++){
            int total = Integer.parseInt(sc.next()) + Integer.parseInt(sc.next());
            System.out.println("Case #"+i+": "+ total);
        }
    }
}