본문 바로가기
반응형

코딩테스트 준비211

백준 C# - 10820 +)풀이 풀이 알고리즘 자체는 쉽게 떠올릴 수 있다. 하지만 입력부분에서 한줄로 입력받는 것이 아니라 한꺼번에 N개의 문자열을 받는 것이 조금 까다로웠다. 입력 첫째 줄부터 N번째 줄까지 문자열이 주어진다. (1 ≤ N ≤ 100) 문자열의 길이는 100을 넘지 않는다. Console.ReadLine은 한줄씩만 받아올 수 있는데 어떻게 할까 하다가 null을 입력받기 전까지 계속 반복문으로 입력을 받자라는 아이디어로 아래와 같이 입력받았다. while ((word = Console.ReadLine()) != null) 코드 전문 using System; using System.Linq; namespace baek2 { class Program { static void Main(string[] args) { str.. 2023. 10. 23.
C# - 배열 초기화 방법 2가지(for문,Enumerable.Repeat ) 배열을 -1로 초기화하는데 3가지 방법이 있다. 01 for루프 사용 int[] vs = new int[26]; for (int i = 0; i < vs.Length; i++) { vs[i] = -1; } 02 Enumerable.Repeat사용 Enumerable.Repeat LINQ라이브러리의 일부로 제공되는 메서드이다. 지정된 요소를 반복하고 생성하여 컬렉션을 만들어준다. public static IEnumerable Repeat(TResult element, int count); 매개변수 TResult : 생성할 요소의 유형 element : 반복해서 생성할 요소 count : 생성할 횟수 아래와 같이 한줄로 배열을 초기화할 수 있다. int[] vs = Enumerable.Repeat(-1, 2.. 2023. 10. 23.
백준 C# - 10809 이 문제를 풀기 전에 아래 문제를 먼저 푸는 것을 추천한다. 백준 C# - 10808 백준 C# - 10808 using System; using System.Collections.Generic; using System.Text; namespace baek2 { class Program { static void Main(string[] args) { string word = Console.ReadLine(); int[] vs = new int[26]; foreach(char c in word) { vs[c - 'a']++; } for(int i = 0; i code-piggy.tistory.com using System; using System.Linq; namespace baek2 { class Pro.. 2023. 10. 23.
백준 C# - 10808 using System; using System.Collections.Generic; using System.Text; namespace baek2 { class Program { static void Main(string[] args) { string word = Console.ReadLine(); int[] vs = new int[26]; foreach(char c in word) { vs[c - 'a']++; } for(int i = 0; i 2023. 10. 23.
백준 C# - 1918 +) 풀이 이 문제를 풀기전에 아래 문제를 먼저 푸는 것을 추천한다. 백준 C# - 1935 백준 C# - 1935 +) 풀이 풀이 후위 표기법으로 계산하는 문제이다. 제시받은 문자열안의 문자들을 순서대로 확인하여 연산자인지 피연산자인지 확인한다. 만약 피연산자인 경우 stack에 push하고 연산자인 경우 두번의 Po code-piggy.tistory.com 중위 표기법에서 후위 표기법으로 바꾸는 문제이다. 중위 표기법에서 후위표기법으로 바꿀때 연선자 우선순위를 고려해야 한다. 01 우선순위 메서드 static int Precedence(char c) { switch (c) { case '+': case '-': return 1; case '*': case '/': return 2; default: return .. 2023. 10. 20.
백준 C# - 1935 +) 풀이 풀이 후위 표기법으로 계산하는 문제이다. 제시받은 문자열안의 문자들을 순서대로 확인하여 연산자인지 피연산자인지 확인한다. 만약 피연산자인 경우 stack에 push하고 연산자인 경우 두번의 Pop을 통해 연산을 수행하도록한다. 예제 문제 풀이 5 ABC*+DE/- 1 2 3 4 5 과정 그림과 같이 흐름만 알게된다면 쉽게 풀 수 있다. 코드 using System; using System.Collections.Generic; namespace baek2 { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string line = Console.ReadLine(); int[] array = new.. 2023. 10. 20.
반응형