Algorithm/이코테
2.곱하기 혹은 더하기
마닐라
2021. 12. 10. 12:55
📍 문제 설명
💡 접근
0이나 1이면 더하기를 나머지이면 곱하기를 수행해주면 되겠다.
👩💻 코드
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String s = kb.nextLine();
int result = 0;
for(char c : s.toCharArray()) {
//더하기
int num = c - '0';
if(num <= 1) {
result += num;
}
//곱하기
else {
if(result == 0) result = 1;
result *= num;
}
}
System.out.println(result);
//0으로 시작 result = 0
//1로 시작 result = 1
//2로 시작 result = 2
//문제 없다.
}
}