백준 - 과제는 끝나지 않아! ( 17592번, JAVA )

changi123·2024년 11월 4일
0
post-thumbnail

Stack ( https://www.acmicpc.net/problem/17952 )

풀이

(1) 점수와 남은 시간을 담을 클래스를 만들었다.
(2) 만약 남은시간이 1이라면 해당 시간을 사용해서 과제를 끝낼 수 있으니까 점수를 더해준다.
(3) 그런게 아니라면 Stack에 시간을 -1 한 상태로 다시 저장


자꾸 95%에서 틀렸습니다가 떠서 뭐가 문제인지 찾다가 맨처음 1이면 점수를 넣어주는 부분에서 st.pop() 을 해서 꺼내주니까 스택이 비어지기 때문에 고쳐서 해결

package problem_solving.stack;

import java.util.Scanner;
import java.util.Stack;

public class BaekJoon_17952 {

	static class At {
		int A = 0 ; 
		int T = 0 ; 
		At(int A , int T ){
			this.A= A;
			this.T = T;
		}
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = Integer.parseInt(sc.next());
		sc.nextLine();
		Stack<At> st = new Stack();
		int score = 0 ; 
		while(t-- > 0 ) {

			String [] arr = sc.nextLine().split(" ");
			if( arr[0].equals("1")) {
				if(arr[2].equals("1")) {
					score+= Integer.parseInt(arr[1]);
					
				}else {
					st.push(new At(Integer.parseInt(arr[1]),Integer.parseInt(arr[2])-1));
				}
			} else {
				if( !st.isEmpty()) {

					At at = st.peek();
					if(at.T == 1) {
						score+= at.A;
						st.pop();
					}else {
						st.pop();
						st.push(new At(at.A,at.T-1));
					}
				} else {
					continue;
				}
			}

		}

		System.out.println(score);
	}

}


profile
개발자 홍찬기 꾸준한 사람이 되자

0개의 댓글

관련 채용 정보