코딩테스트/JAVA
[ 백준 ] 11382번 : 꼬마 정민 _ JAVA
어서오시우
2023. 11. 25. 09:42
728x90
문제
- 1 ≤ A, B, C ≤ 10¹² 로 숫자의 범위가 주어졌으므로, int 형을 쓰면 틀린다.
풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
int C = Integer.parseInt(st.nextToken());
System.out.println(A + B + C);
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
int C = Integer.parseInt(st.nextToken());
System.out.println(A + B + C);
}
}
- 내 풀이에서는 int 형을 작성했으므로 틀렸다.
- 참고로 위 문제에서 입력값 77 77 7777의 경우에는 int 자료형으로도 처리가 가능하다.
그러나 문제 조건에 따라 long 형을 사용하도록 하자.- int 자료형이 표현할 수 있는 값 범위
- 최소값: -2,147,483,648 (-2^31)
- 최대값: 2,147,483,647 (2^31 - 1)
- int 자료형이 표현할 수 있는 값 범위
- 그리고 위 코드보다 간결하게 작성할 수 있다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
sc.close();
System.out.println(a + b + c);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
sc.close();
System.out.println(a + b + c);
}
}