코딩테스트
백준 10773 java
미소5
2023. 7. 9. 14:05
728x90
반응형
- a[i]에 바로 입력하면 안된다!
- n에 입력한 후 n이 0일 경우 top을 1 줄이고, 아닐 경우 top을 1 늘린다.
- int top=-1; //마지막 원소 위치
- top이 가리키는 값만 변경해주면 된다!
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int k=sc.nextInt();
int[] a=new int[k];
int sum=0;
int top=-1;
for(int i=0; i<k; i++) {
int n=sc.nextInt();
if(n==0) {
top--;
}
else {
top++;
a[top]=n;
}
}
for(int i=0; i<=top; i++) {
sum+=a[i];
}
System.out.println(sum);
}
}
728x90
반응형