Archives
Recent Posts
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
11번가
Today
Total
05-17 21:05
관리 메뉴

smwhee

[코딩도장_문제120] Dash Insert 본문

Development

[코딩도장_문제120] Dash Insert

smwhee 2017. 9. 26. 15:33

package codingDojang;


/*

 * Dash Insert

 * 

 * DashInsert 함수는 숫자로 구성된 문자열을 입력받은 뒤, 문자열 내에서 홀수가 연속되면 두 수 사이에 - 를 추가하고, 짝수가 연속되면 * 를 추가하는 기능을 갖고 있다. (예, 454 => 454, 4546793 => 454*67-9-3) DashInsert 함수를 완성하자. 출처

 * 

 * 입력 - 화면에서 숫자로 된 문자열을 입력받는다.

 * "4546793"

 * 출력 - *, -가 적절히 추가된 문자열을 화면에 출력한다.

 * "454*67-9-3"

 */


public class Question_120 {


public static void main(String[] args) {

String str = "4546793";

String result = "";

for(int i = 0; i < str.length(); i++) {

int mod = Integer.parseInt(String.valueOf(str.charAt(i))) % 2;


if(i > 0) {

int temp = Integer.parseInt(String.valueOf(str.charAt(i - 1))) % 2;

if(mod == temp) {

if(mod == 1) {

result += "-";

}

if(mod == 0) {

result += "*";

}

}

}

result += String.valueOf(str.charAt(i));

}

System.out.println(result);

}


}



Comments