C# 8

C# - 거스름돈

- 개선 사항 : 변수명 수정한다면 ! 틀린 것 1./= : 나눗셈 후 할당=/ : 존재하지 않는 연산자 틀린 것 2. foreach : 컬렉션을 순회할 때 사용 (foreach num in nums)-> 내가 하고 싶었던 것 : while 3. return : 한 번만 계산하고 끝냄-> continue를 사용하거나, return을 제거하거나 4. 조건문 : remain > 500이 아니라, remain >= 500으로 해야함 5. 몫 : / 로 구하고, 나머지는 %로 구해야함 6. intInput지불 금액, remain(뺀 값)이랑 잘 구분해서 사용하기 7. return count; 위치while 루프 안 -> 도달할 수 없음 => while 밖으로 빼야 함 8. while 조건 remai..

C#/복습 필요 2025.10.07

문자열 정렬하기 (2) (복습 완료)

개선 코드* 일반 코드 using System;public class Solution { public string solution(string my_string) { char[] chars = my_string.ToLower().ToCharArray(); Array.Sort(chars); return new string(chars); }}1. 나 : 대문자만 변환 / 모든 문자를 변환해도 괜찮음 (불필요한 조건)2. string 내장 메서드 사용해서 소문자로 변환-> 반복문없이 문자열의 문자들 소문자로 변환 가능 +) string의 내장 메서드들// String 내장 메서드들my_string.ToLower(); // 소문자 변환m..

C#/복습 완료 2025.09.02

주사위의 개수 (복습 완료)

개선 코드직접 계산반복문 안 쓰고, 배열 인덱스로 직접 접근 (가로 * 세로 * 높이) using System;public class Solution { public int solution(int[] box, int n) { return (box[0] / n) * (box[1] / n) * (box[2] / n); }} LINQ 사용using System;using System.Linq;public class Solution { public int solution(int[] box, int n) { return box.Select(length => length / n).Aggregate(1, (a, b) => a * b); }}box.Select(length..

C#/복습 완료 2025.08.21

직각삼각형 출력하기 (복습 완료)

개선된 코드 이중 반복문 -> 단일 반복문for (int i = 1; i new string('*', i) : *를 i개 만듦 ! cf) 참고 : 다른 한 줄 방법들 LINQusing System;using System.Linq; // LINQ가 아니라! public class Example{ public static void Main() { int n = int.Parse(Console.ReadLine()); Enumerable.Range(1, n).ToList() .ForEach(i => Console.WriteLine(new string('*', i))); }}- Enumerable.Range(1, n) : 1부터 n까지 숫..

C#/복습 완료 2025.08.14

[C#]숨어있는 숫자의 덧셈(1) - 복습 완료

개선 코드LINQ 버전using System;using System.Linq;public class Solution { public int solution(string my_string) { return my_string.Where(char.IsDigit).Sum(ch => ch - '0'); }}- Where(char.IsDigit) : char.IsDigit 메서드를 각 문자에 적용해서 숫자인 것만 필터링- Sum(ch => ch - '0') : 필터링된 각 char에 대해 ch - '0' 변환 후 합계 +) char.IsDigit(string, index) 사용해서 푼 버전* index가 필요하므로, foreach말고 for문 쓰 (가능하긴함)using System;usi..

C#/복습 완료 2025.08.07

[C#]문자 반복 출력하기 (프로그래머스) - (복습 완료)

개선 코드* Linqusing System;using System.Linq;public class Solution { public string solution(string my_string, int n) { return string.Concat(my_string.Select(ch => new string(ch, n))); }}.Select : 문자열의 각 문자(ch)를 변환ch => new string(ch, n) : 각 문자를 n번 반복한 문자열로 변환.Concat : 변환된 문자열을 하나로 합침 => 문자열의 각 문자를 -> 그 문자를 n번 반복한 문자열로 바꿔서 -> 모두 합쳐줘 cf. 다른 LINQ 방법return new string(my_string.SelectMany(c..

C#/복습 완료 2025.08.07

[C#]분수의 덧셈(프로그래머스) - 복습 필요(9/8)

개선 코드 또 새로푼 것 * 에러-> 눈을 씻고 찾아봐도 21번째 무리가 없는데... 20번째에서 ;을 빠뜨렸다 ! 시도using System;// 입력 : 첫 번째 분수 분자 nemer1, 분모 denom1 / 분자 numer2, denom2 // 출력 : 두 분수를 더한 분수 -> 기약 분수 / 정수 분자, 정수 분모 배열 answer public class Solution { public int[] solution(int numer1, int denom1, int numer2, int denom2) { //int[] answer = new int[] {}; int tempNumer = 0; int tempDenom = 0; ..

C#/복습 필요 2025.08.04