https://www.acmicpc.net/problem/10773
10773번: 제로
첫 번째 줄에 정수 K가 주어진다. (1 ≤ K ≤ 100,000) 이후 K개의 줄에 정수가 1개씩 주어진다. 정수는 0에서 1,000,000 사이의 값을 가지며, 정수가 "0" 일 경우에는 가장 최근에 쓴 수를 지우고, 아닐 경
www.acmicpc.net
최근의 수를 지운다는 점에서, stack을 사용하면 될 것 같았다. 역대급으로 빨리 푼 문제같다!
using System;
namespace B10773
{
class Program
{
static void Main()
{
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
int k = int.Parse(sr.ReadLine());
Stack<int> stack = new Stack<int>();
int input;
for(int i = 0; i < k; i++)
{
input= int.Parse(sr.ReadLine());
if(input == 0) stack.Pop();
else stack.Push(input);
}
int result = stack.Sum();
sw.WriteLine(result);
sr.Close();
sw.Flush();
sw.Close();
}
}
}
CLASS 2 달성!!!!
'알고리즘 > 백준 BOJ' 카테고리의 다른 글
[ BOJ/C# ] 8958 OX퀴즈 (0) | 2023.09.09 |
---|---|
[ BOJ/C# ] 10809 알파벳 찾기 (1) | 2023.09.07 |
[ BOJ/C# ] 1874 스택 수열 (0) | 2023.09.05 |
[ BOJ/C# ] 2164 카드2 (0) | 2023.09.05 |
[ BOJ/C# ] 1920 수 찾기 (0) | 2023.09.03 |