하루 한 접시
[백준] 10773번: 제로 [C#]
NaZZU
2024. 4. 1. 23:23
N회의 반복을 진행하며, 0이 아닌 정수가 입력되면 저장하고, 0이 입력되면 맨 위의 정수를 제거한다.
모든 입력이 끝난 후, 남은 정수들의 합을 출력한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
namespace 연습장
{
internal class Program
{
static List<int> stack = new List<int>();
static StringBuilder sb = new StringBuilder();
static void Main(string[] args)
{
using var reader = new StreamReader(Console.OpenStandardInput());
using var print = new StreamWriter(Console.OpenStandardOutput());
int N = int.Parse(reader.ReadLine());
for (int i = 0; i < N; i++)
{
int input = int.Parse(Console.ReadLine());
if (input == 0)
stack.RemoveAt(stack.Count - 1);
else
stack.Add(input);
}
int count = 0;
for (int j = 0; j < stack.Count(); j++)
{
count += stack[j];
}
print.WriteLine(count);
}
}
}
|
cs |
스택을 조금 더 파고 들어간 문제인데, 나는 아직 스택을 안배워서 그냥 리스트로 구현했다.