728x90
반응형
백준알고리즘
- Bronze 4 -
#15873. 공백 없는 A+B by JAVA and node.js
문제
출처: https://www.acmicpc.net/problem/15873
접근 방법
A와 B의 범위가 0이상 10이하이므로 3가지 경우에 따라 계산하면 된다.
- 둘 다 한 자리수일 경우 > A: 10으로 나눈 몫, B: 10으로 나눈 나머지
- 둘 중 하나가 10일 경우 > 입력값.replaceAll("10","") + 10
- 둘 다 10일 경우 > 20
입력은 String으로 받는다. 또한 시간제한이 0.5초인만큼 BufferedReader를 사용한다.
풀이
▶ JAVA
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
br.close();
int result = 0;
if("1010".equals(input))
result = 20;
else if(input.contains("10"))
result = Integer.parseInt(input.replaceAll("10",""))+10;
else
result = Integer.parseInt(input) / 10 + Integer.parseInt(input) % 10;
System.out.println(result);
}
}
▶ node.js
node.js에서는 contains()를 쓸 수 없으므로, 아래의 경우에 따라 나눈 후
- input == "1010"인지
- input % 100 == 10인지,(B = 10)인지
나머지의 경우엔 A가 10이어도 관련 없으므로 java와 동일하게 출력하면된다.
형변환에만 주의하자.
var fs = require('fs');
var input = fs.readFileSync('/dev/stdin').toString().trim();
var result = 0;
if(input == "1010")
result = 20;
else if(parseInt(input) % 100 == 10)
result = parseInt(input.replaceAll("10","")) + 10;
else
result = parseInt(parseInt(input) / 10) + parseInt(input) % 10;
console.log(result);
결과
End.
heisely's 괴발개발 개발일지
728x90
반응형
'알고리즘 > 백준알고리즘' 카테고리의 다른 글
[백준알고리즘] #15921. 수찬은 마린보이야!! (by JAVA and node.js) (0) | 2022.05.30 |
---|---|
[백준알고리즘] #8545. Zadanie próbne (by JAVA and node.js) (0) | 2022.05.30 |
[백준알고리즘] #15726. 이칙연산(by JAVA and node.js) (0) | 2022.05.05 |
[백준알고리즘] #15700. 타일 채우기 4 (by JAVA and node.js) (0) | 2022.05.05 |
[백준알고리즘] #15610. Abbey Courtyard (by JAVA and node.js) (0) | 2022.05.03 |