[C#] SortedSet 중복 없는 배열 생성
·
언어/C#
백준 25192번 문제를 풀면서 사용한 SortedSet은 중복 없는 배열을 알아서 만들어줍니다. using System; class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int i, num = 0; string enter = null; SortedSet s = new SortedSet(); enter = Console.ReadLine(); for (i = 0; i < n-1; i++) { enter = Console.ReadLine(); if (enter == "ENTER") { num += s.Count(); s.Clear(); } else { s.Add(enter); } } num += s..
[C#] 백준 10989번 : StreamReader
·
언어/C#
using System; class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); int[] arr = new int[10001]; for (int i = 0; i < N; i++) arr[int.Parse(Console.ReadLine())]++; for (int i = 0; i < 10000; i++) { if (arr[i] != 0) { for (int j = 0; j < arr[i]; j++) Console.WriteLine(i); } } } } 메모리를 아끼기 위해 2751번에서 사용했던 Array.Sort 방식이 아니라 다른 방식을 사용해야 했다. 구글링을 통해서 필요한 최대 크기의 배열을 미..
[C#] 백준 1929번 : 에라토스테네스의 체 방식(빠르게 소수 찾기)
·
언어/C#
using System; using System.Text; class Program { static void Main(string[] args) { StringBuilder sb = new StringBuilder(); string[] s = Console.ReadLine().Split(); int M = int.Parse(s[0]); int N = int.Parse(s[1]); bool OK = true; if (M == 1) M++; for (int i = M; i < N; i++) { OK = true; for (int j = 2; j
[C#] 백준 2581번
·
언어/C#
using System; class Program { static void Main(string[] args) { int M = int.Parse(Console.ReadLine()); int N = int.Parse(Console.ReadLine()); int min = 0; int sum = 0; bool[] arr = new bool[N + 1]; bool ok = false; for (int i = M; i
[C#] 백준 10757번 : 큰 수를 다룰 땐 BigInteger를 활용하자
·
언어/C#
백준 10757번문제다. int, long으로는 답을 구할 수 없다. 이땐 BigInteger로 변수를 지정해주면 큰 수를 다룰 수 있다.👍 using System; using System.Net.Http.Headers; using System.Numerics; using System.Text; class Program { static void Main(string[] args) { string[] s = Console.ReadLine().Split(); BigInteger a = BigInteger.Parse(s[0]); BigInteger b = BigInteger.Parse(s[1]); Console.WriteLine(a + b); } }
[C#] 백준 2839번
·
언어/C#
using System; using System.Net.Http.Headers; class Program { static void Main(string[] args) { /* 1. N이 5의 배수 * 2. N이 3의 배수 * 3. N을 5로 나눴는데 나머지가 3의 배수 * 4. N을 3으로 나눴는데 나머지가 5의 배수 */ int N = int.Parse(Console.ReadLine()); int count = 0; while (N > 0) { if (N % 5 == 0) { N -= 5; count++; } else if (N % 3 == 0) { N -= 3; count++; } else if (N > 5) { N -= 5; count++; } else { count = -1; break; } } ..
[C#] 백준 2292번
·
언어/C#
using System; class Program { static void Main(string[] args) { int S = int.Parse(Console.ReadLine()); int N = 1; int i = 1; if (S == 1) Console.WriteLine(1); else { while (true) { N += 6 * i; if (S
[C#] 백준 1712번
·
언어/C#
using System; class Program { static void Main(string[] args) { string[] arr = Console.ReadLine().Split(); int A = int.Parse(arr[0]); int B = int.Parse(arr[1]); int C = int.Parse(arr[2]); int x = 1; if (B >= C) Console.WriteLine(-1); else { Console.WriteLine(A / (C - B) + 1); } } } 손익분기점이 존재하지 않는 부분은 가변비용(B)이 판매비용(C)와 같거나 높은 경우다. 판매비용(x)를 구하는 단순한 식을 통해 x를 구하고 출력한다.
[C#] StringBuilder란
·
언어/C#
string a = "안녕"; string b = "Hi"; Console.WriteLine(a + b); 기존 string을 사용하여 문자열을 합치면 a와 b각각의 instance가 생성되는 낭비가 발생한다. StringBuilder sum = new StringBuilder(); sum.Append("안녕"); sum.Append("Hi"); Console.WriteLine(sum.ToString()); StringBuilder를 사용하면 새로운 변수가 생성될 필요 없이 값을 추가할 수 있어서 낭비가 없다. 아주 적은 양의 문자열을 사용할 경우는 상관 없지만 많은 양을 사용할 경우 StringBuilder를 사용해야 한다.
[C#] 구조체(struct)란?
·
언어/C#
구조체(struct)란 데이터 관련 기능을 캡슐화해놓는 형식이다. https://codingjin0424.tistory.com/18 [C] 구조체 - 고양이 뽑기 게임 #include #include #include typedef struct { char* name; int age; char* character; int level; } CAT; int collection[5] = { 0,0,0,0,0 }; CAT cats[5]; void initCats(); void printCat(int collect); int c.. codingjin0424.tistory.com 예전에 했던 고양이 뽑기 게임처럼 내가 원하는 고양이의 정보를 모아놓은 구조체를 통해 고양이 정보를 사용할 수 있다.
[C#] 델리게이트 체인(delegate chain) 단순한 예제
·
언어/C#
델리게이트 객체 내에 여러 메서드에 대한 리스트 정보를 가지게 하는 방법을 델리게이트 체인이라고 한다. static void Main(string[] args) { MyDel myDel = new MyDel(test1); myDel = new MyDel(test2); myDel = new MyDel(test3); myDel(); } 보통 우리가 myDel에 test를 담고 싶으면 =을 사용한다. 위에 내용을 실행하면 이렇게 마지막에 담은 test3의 "세 번째"라는 내용만 출력된다. 만약 내가 test1과 test2에 담긴 내용도 다 보고 싶다면 =을 +=로 변경해주기만 하면 된다. static void Main(string[] args) { MyDel myDel = new MyDel(test1); my..
[C#] 인터페이스(Interface)란?
·
언어/C#
추상클래스를 이용하다 보면 2개 이상의 클래스를 상속받고 싶을 때가 있다. class Program { public abstract class Sky { public abstract void Fly(); } public abstract class Water { public abstract void Swim(); } public class Monster : Sky, Water // 에러 { public override void Fly() { Console.WriteLine("날기"); } public override void Swim() // 에러 { Console.WriteLine("수영중"); } } 하늘에서는 날고 물에서는 수영하는 몬스터를 만들고 싶지만, 추상 클래스로는 2개 이상의 클래스를 상속받..
[C#] Span이란?
·
언어/C#
ArraySegment와 비슷하게 Span도 배열의 일부를 가져오는 구조체다. ArraySegment보다 활용성이 더 높다. static void Main(string[] args) { int[] arr = { 1, 2, 3, 4, 5 }; int start = 0; int end = 3; Span span = new Span(arr, start, end); Console.WriteLine(span[2]); } 3이 성공적으로 출력된다. ArraySegment와 비슷하게 시작점과 길이를 정해줄 수 있다. 읽기 전용 메서드인 ReadOnlySpan도 있다. 수정 불가하니 더 안전하다. Span의 하위 집단을 가리키는 데에 Span.Slice를 활용할 수도 있다.
[C#] ArraySegment를 이용하여 배열 가져오기
·
언어/C#
배열 안에 일부를 가져오기 위해서 새로운 배열을 파는 것 말고도 ArraySegment를 사용하는 방법이 있다. static void Main(string[] args) { int[] arr = { 1, 2, 3, 4, 5 }; int start = 0; int end = 3; ArraySegment segment = new ArraySegment(arr, start, end); Console.WriteLine(String.Join(", ", segment)); // 배열을 이어붙일 수 있는 간단한 메서드. } ArraySegment의 start는 Offset을 의미한다. 그대로 받아들이면 배열의 변화량을 의미하지만 쉽게 이해하기 위해서는 숫자를 가져올 배열의 시작점을 몇 번째로 할 거냐는 의미다. en..
[C#] 바이트 배열을 int로, int를 바이트 배열로
·
언어/C#
using System; using System.Threading; namespace Study { class Program { static void Main(string[] args) { byte[] bytes = { 0, 0, 0, 25 }; if (BitConverter.IsLittleEndian) // 오른쪽 끝에 중요한 게 있다 Array.Reverse(bytes); // 배열을 반대로 정렬 (중요한 걸 왼쪽 끝으로 이동) int i = BitConverter.ToInt32(bytes, 0); Console.WriteLine($"byte -> int : {i}"); byte[] bytes2 = BitConverter.GetBytes(2687511146); Console.WriteLine("int..