728x90
반응형
* 출처 SWEA
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
내 생각
1. 첫번째 풀이
단순한 큐 문제! 다만, 여기서 이중 반복문을 2번 방법에서는 단일 반복문으로 바꿔보았다.
public class SWEA_1225 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int T = 10;
for(int tc=1;tc<=T;tc++) {
int tcNum = Integer.parseInt(in.readLine());
Queue<Integer> queue = new LinkedList<>();
st = new StringTokenizer(in.readLine());
//init queue
for(int i=0;i<8;i++) {
int num = Integer.parseInt(st.nextToken());
queue.offer(num);
}
int qPoll = -1;
boolean isZero = false;
while(true) {
for(int i=1;i<=5;i++) {
qPoll = queue.poll();
qPoll -= i;
if(qPoll <= 0) {
qPoll = 0;
queue.offer(qPoll);
isZero = true;
break;
}
queue.offer(qPoll);
}
if(isZero) break;
}
System.out.print("#"+tc+" ");
for(int i=0;i<8;i++) {
System.out.print(queue.poll()+" ");
}
System.out.println();
}
}
}
2. 두번째 풀이
위의 이중 반복문을 아래와 같이 바꾼다.
모듈 연산자를 이용해서 계속 반복할 수 있다는 점을 기억하자!
int minus = 0;
while (true) {
minus = minus % 5 + 1;
qPoll = queue.poll();
qPoll -= minus;
if(qPoll<=0) {
qPoll = 0;
queue.offer(qPoll);
break;
}
queue.offer(qPoll);
}
or
int sub = 1;
int number = -1;
while (number != 0) {
number = q.poll();
number -= sub;
if(number<=0) number = 0;
q.offer(number);
sub = sub % 5 + 1;
}
728x90
반응형
'알고리즘 > swea' 카테고리의 다른 글
[문제] SWEA 1861번 정사각형 방 (0) | 2021.02.07 |
---|---|
[문제] SWEA 3499번 퍼펙트 셔플 (0) | 2021.02.05 |
[문제] SWEA 2001번 파리 퇴치 (0) | 2021.02.03 |
[문제] swea 2805번 농작물 수확하기 (0) | 2021.02.03 |
[문제] SWEA 1208번 (S/W 문제해결 기본) 1일차 - Flatten (0) | 2021.02.03 |