728x90
언어_자바
프로그래머스_이어 붙인 수
public class OddEven {
public static void main(String[] args) {
Test2 t = new Test2();
int[] num_list = {3,4,5,2,1};
t.test2(num_list);
}
}
class Test2{
public int test2(int[] num_list) {
int answer = 0;
String odd = "";
String even = "";
for(int i=0; i<num_list.length; i++){
if(num_list[i] % 2 == 0){
odd += String.valueOf(num_list[i]); // 이어 붙이기 위해 문자열로 만들기
}else{
even += String.valueOf(num_list[i]);
}
}
answer = Integer.parseInt(odd)+Integer.parseInt(even); // 홀과 짝을 더할 때 정수로 더하기
System.out.println(answer);
return answer;
}
}
홀 짝의 합에 관해서 String 으로 저장하는 것이 Key Point
✔ String.valueOf 와 Integer.parseInt 를 사용하였다.
728x90
'Algorithm' 카테고리의 다른 글
[프로그래머스] (181925) 수 조작하기2 (0) | 2023.12.24 |
---|---|
[프로그래머스] (181926) 수 조작하기1 (0) | 2023.12.24 |
[프로그래머스] (181927) 마지막 두 원소 (0) | 2023.12.24 |
[프로그래머스] (181932) 코드 처리하기 (0) | 2023.12.23 |
[백준](15552) 빠른 A+B (0) | 2023.12.19 |