풀이과정
#1. 처음시도
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
System.out.println(n/m);
System.out.println(n%m);
}
}
주의할점
- 띄어쓰기 입력받을 때, split함수로 공백기준나누고.. 문자열을 parseInt하는.. 수고로움을 겪을 필요 없음. 저렇게 nextInt로 따로 써주면 알아서 공백 기준으로 n,m에 값 대입해줌
- 문제의 조건을 잘보자..;;
java에서 제일 열받는것 중 하나가 int, long이 문제에서 요구하는 범위를 감당하지 못한다는 것이다.
이럴 때 진짜 짱남
저렇게 제출했을 때 자꾸 런타임에러가 나길래, 노트북이랑 짱뜨기 직전까지 갔다가 설마 또 BigInteger..?하고 범위를 봤는데, 역시나였다
#2 2차시도
import java.math.BigInteger;
import java.util.*;
public class bronzetest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// int n = sc.nextInt();
// int m = sc.n
//
// System.out.println(n/m);
// System.out.println(n%m);
BigInteger n,m;
n = sc.nextBigInteger();
m = sc.nextBigInteger();
System.out.println(n.divide(m));
System.out.println(n.remainder(m));
}
}
이렇게 BigInteger로 자료형을 맞춰주면, 또 안타깝게도 int에서 사용할 수 있는 '/' , '%'이런 연산자를 또 쓸 수가 없다ㅠㅠ
그래서 저렇게 함수로 직접 연산해주면 된다.
https://www.acmicpc.net/problem/1271
1271번: 엄청난 부자2
첫째 줄에는 최백준 조교가 가진 돈 n과 돈을 받으러 온 생명체의 수 m이 주어진다. (1 ≤ m ≤ n ≤ 101000, m과 n은 10진수 정수)
www.acmicpc.net
728x90
'백준이당' 카테고리의 다른 글
[JAVA] 백준 2869번 : 달팽이는 올라가고 싶다. (0) | 2023.06.04 |
---|---|
[JAVA] 백준 1978번 : 소수찾기 (0) | 2023.06.02 |
[JAVA] 백준 1037: 약수 (0) | 2023.05.31 |
[JAVA] 백준 1009 : 분산처리 (0) | 2023.05.31 |
[JAVA] 백준 3003 : 킹, 퀸, 룩, 비숍, 나이트, 폰 (0) | 2023.05.29 |