본문 바로가기

문제 풀이49

Lv.0 Java) 대소문자 바꿔서 출력하기 https://school.programmers.co.kr/learn/courses/30/lessons/181949 이 문제를 풀기 위해서는 아래의 세 방법을 제시할 수 있어야 한다.1. 문자열을 순회하는 방법2. 대소문자를 판별하는 방법3. 대소문자를 서로 변환하는 방법 더보기import java.util.Scanner;public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String a = sc.next(); char diff = 'a' - 'A'; for (char c : a.toCharArray()) .. 2024. 12. 16.
Lv.0 Java) 문자열 반복해서 출력하기 https://school.programmers.co.kr/learn/courses/30/lessons/181950 문자열 str을 n번 반복해서 출력하는 간단한 문제이다.반복문을 이용해서 풀 수도 있지만 api를 알고 있다면 코드를 간략하게 작성할 수 있다. 더보기import java.util.Scanner;public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); int n = sc.nextInt(); System.out.println(str.repeat(n)); .. 2024. 12. 15.
백준 15652 C#) N과 M (4) 이전에 풀었던 N과 M (1)과 거의 유사한 문제다. class Program { static StreamWriter sw = new(new BufferedStream(Console.OpenStandardOutput())); static int n, m; static int[] result; static void DFS(int depth, int start) { if (depth == m) { sw.WriteLine(string.Join(' ', result)); return; } for (int i = start; i 2023. 3. 8.
백준 15649 C#) N과 M (1) 백트래킹 및 DFS의 기본을 익힐 수 있는 문제. 시리즈가 굉장히 많으므로 틈틈이 하나씩 풀면 좋다. class Program { static StreamWriter sw = new(new BufferedStream(Console.OpenStandardOutput())); static int n, m; static int[] result; static bool[] visited; static void DFS(int depth) { if (depth == m) { sw.WriteLine(string.Join(' ', result)); return; } for (int i = 1; i 2023. 3. 8.
백준 1002 C#) 터렛 간단한 중~고등학교 수준의 수학 문제. 두 원의 교점 개수를 묻는 문제였다. class Program { static void Main() { StreamWriter sw = new(new BufferedStream(Console.OpenStandardOutput())); int t; int[] xyr; int x1, y1, r1; int x2, y2, r2; double dist; t = int.Parse(Console.ReadLine()); while (t-- > 0) { xyr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse); (x1, y1, r1) = (xyr[0], xyr[1], xyr[2]); (x2, y2, r2) = (xyr[3], x.. 2023. 3. 7.
백준 18870 C#) 좌표 압축 요약하자면, 주어진 숫자가 몇번째로 작은지를 출력하는 문제이다. class Program { static void Main() { StreamWriter sw = new(new BufferedStream(Console.OpenStandardOutput())); //입력 int n = int.Parse(Console.ReadLine()); string[] x = Console.ReadLine().Split(); //압축된 좌표를 기존 위치에 출력하려면 기존 위치를 기억해야 하므로, //입력받은 x(num)를 기존 위치(oldIdx)와 함께 ValueTuple로 묶어 저장한다(input) (int num, int oldIdx)[] input = new (int, int)[n]; for (int i = 0; .. 2022. 12. 30.
백준 11651 C#) 좌표 정렬하기 2 이전 문제처럼 ValueTuple을 사용해서 풀어보자. *IComparable, IComparer에 대해 모른다면 이 글을 먼저 읽어보자. IComparer.Compare(T, T) 메서드 (System.Collections.Generic) 두 개체를 비교하여 한 개체가 다른 개체보다 작거나, 같거나 또는 크다는 것을 나타내는 값을 반환합니다. learn.microsoft.com 이 문제는 11650번 문제와 동일한 오름차순 정렬이지만 x가 아닌 y를 우선시하고 있다. 따라서 Compare 메서드는 아래의 기준으로 작성할 수 있다. 1) y가 같다면 x를 기준으로 정렬하라 2) y가 다르다면 y를 기준으로 정렬하라 반환값은 뺄셈으로 쉽게 구할 수 있다. 2022. 10. 12.
백준 11650 C#) 좌표 정렬하기 ValueTuple을 활용하면 쉽게 풀 수 있다. public static class PS { public static void Main() { StreamWriter sw = new(new BufferedStream(Console.OpenStandardOutput())); int n = int.Parse(Console.ReadLine()); (int x, int y)[] arr = new (int x, int y)[n]; string[] xy; for (int i = 0; i < n; i++) { xy = Console.ReadLine().Split(); arr[i] = (int.Parse(xy[0]), int.Parse(xy[1])); } Array.Sort(arr); for (int i = 0; i.. 2022. 10. 12.