문제 내용 자체가 어렵지 않아서 몇 분 안되어 풀이를 했다.
문제 제한 조건 중
FoodFight.java
package com.example.Programmers.Lv1;
/**
* 프로그래머스 Lv1 - 푸드 파이트 대회
*/
public class FoodFight {
public String solution(int[] food) {
String answer = "";
StringBuilder sb = new StringBuilder();
for (int i = 1; i < food.length; i++) {
int quotient = food[i] / 2;
for (int j = 1; j <= quotient; j++) {
sb.append(i);
}
}
String origin = sb.toString();
String reversed = sb.reverse().toString();
answer = origin + "0" + reversed;
return answer;
}
}
FoodFightTest.java
package com.example.Programmers.Lv1;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class FoodFightTest {
@Test
public void testFoodFight() {
FoodFight ff = new FoodFight();
String result1 = ff.solution(new int[] { 1, 3, 4, 6 });
String result2 = ff.solution(new int[] { 1, 7, 1, 2 });
assertEquals("1223330333221", result1);
assertEquals("111303111", result2);
}
}