- C# #27 알고리즘 코드카타 22023년 09월 02일 23시 16분 17초에 업로드 된 글입니다.작성자: noun06
자연수 뒤집어 배열로 만들기
- 자연수 n을 뒤집어 각 자리 숫자를 원소로 가지는 배열 형태로 리턴해주세요. 예를들어 n이 12345이면 [5,4,3,2,1]을 리턴합니다.
using System; public class Solution { public int[] solution(long n) { string s = n.ToString(); int[] answer = new int[s.Length]; int count=0; while(n>0) { answer[count]=(int)(n%10); n/=10; count++; } return answer; } }
문자열을 정수로 바꾸기
문자열 s를 숫자로 변환한 결과를 반환하는 함수, solution을 완성하세요.
public class Solution { public int solution(string s) { int answer = 0; answer = int.Parse(s); return answer; } }
정수 제곱근 판별
임의의 양의 정수 n에 대해, n이 어떤 양의 정수 x의 제곱인지 아닌지 판단하려 합니다.
n이 양의 정수 x의 제곱이라면 x+1의 제곱을 리턴하고, n이 양의 정수 x의 제곱이 아니라면 -1을 리턴하는 함수를 완성하세요.class Solution { public long solution(long n) { long answer = 0; for(long i=1;i<=n;i++){ if(i*i==n){ answer =(i+1)*(i+1); break; }else answer =-1; } return answer; } }
정수 내림차순으로 배치하기
함수 solution은 정수 n을 매개변수로 입력받습니다. n의 각 자릿수를 큰것부터 작은 순으로 정렬한 새로운 정수를 리턴해주세요. 예를들어 n이 118372면 873211을 리턴하면 됩니다.
public class Solution { public long solution(long n) { char[] arr = n.ToString().ToCharArray(); for (int i = 0; i < arr.Length - 1; i++) { for (int j = 0; j < arr.Length - i - 1; j++) { if (arr[j] < arr[j + 1]) { char temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } string sortedNumberStr = new string(arr); long answer = long.Parse(sortedNumberStr); return answer; } }
하샤드 수
양의 정수 x가 하샤드 수이려면 x의 자릿수의 합으로 x가 나누어져야 합니다. 예를 들어 18의 자릿수 합은 1+8=9이고, 18은 9로 나누어 떨어지므로 18은 하샤드 수입니다. 자연수 x를 입력받아 x가 하샤드 수인지 아닌지 검사하는 함수, solution을 완성해주세요.
public class Solution { public bool solution(int x) { bool answer = true; int sum = 0; int n = x; while(x > 0){ sum += x % 10; x /= 10; } if(n % sum == 0){ answer = true; }else{ answer = false; } return answer; } }
두 정수 사이의 합
두 정수 a, b가 주어졌을 때 a와 b 사이에 속한 모든 정수의 합을 리턴하는 함수, solution을 완성하세요.
예를 들어 a = 3, b = 5인 경우, 3 + 4 + 5 = 12이므로 12를 리턴합니다.public class Solution { public long solution(int a, int b) { long answer = 0; if(a > b){ int temp = a; a = b; b = temp; } for(int i = a; i <= b; i++){ answer += i; } return answer; } }
콜라츠 추측
1937년 Collatz란 사람에 의해 제기된 이 추측은, 주어진 수가 1이 될 때까지 다음 작업을 반복하면, 모든 수를 1로 만들 수 있다는 추측입니다. 작업은 다음과 같습니다.
1-1. 입력된 수가 짝수라면 2로 나눕니다. 1-2. 입력된 수가 홀수라면 3을 곱하고 1을 더합니다. 2. 결과로 나온 수에 같은 작업을 1이 될 때까지 반복합니다.
public class Solution { public int solution(long num) { int count = 0; while (num != 1) { if (count == 500) { count = -1; break; } num = num % 2 == 0 ? num / 2 : num * 3 + 1; count++; } return count; } }
서울에서 김서방 찾기
String형 배열 seoul의 element중 "Kim"의 위치 x를 찾아, "김서방은 x에 있다"는 String을 반환하는 함수, solution을 완성하세요. seoul에 "Kim"은 오직 한 번만 나타나며 잘못된 값이 입력되는 경우는 없습니다.
public class Solution { public string solution(string[] seoul) { string answer = ""; int num = 0; for(int i = 0; i<seoul.Length;i++){ if(seoul[i]=="Kim"){ num = i; } } answer = $"김서방은 {num}에 있다"; return answer; } }
나누어 떨어지는 숫자
array의 각 element 중 divisor로 나누어 떨어지는 값을 오름차순으로 정렬한 배열을 반환하는 함수, solution을 작성해주세요.
divisor로 나누어 떨어지는 element가 하나도 없다면 배열에 -1을 담아 반환하세요.using System.Collections.Generic; public class Solution { public int[] solution(int[] arr, int divisor) { List<int> answer = new List<int>(); foreach (var item in arr) { if (item % divisor == 0) answer.Add(item); } if (answer.Count == 0) answer.Add(-1); else answer.Sort(); return answer.ToArray(); } }
음양 더하기
어떤 정수들이 있습니다. 이 정수들의 절댓값을 차례대로 담은 정수 배열 absolutes와 이 정수들의 부호를 차례대로 담은 불리언 배열 signs가 매개변수로 주어집니다. 실제 정수들의 합을 구하여 return 하도록 solution 함수를 완성해주세요.
using System; public class Solution { public int solution(int[] absolutes, bool[] signs) { int answer = 0; for(int i = 0; i < absolutes.Length; i++){ if(signs[i]){ answer += absolutes[i]; }else{ answer -= absolutes[i]; } } return answer; } }
핸드폰 번호 가리기
프로그래머스 모바일은 개인정보 보호를 위해 고지서를 보낼 때 고객들의 전화번호의 일부를 가립니다.
전화번호가 문자열 phone_number로 주어졌을 때, 전화번호의 뒷 4자리를 제외한 나머지 숫자를 전부 *으로 가린 문자열을 리턴하는 함수, solution을 완성해주세요.public class Solution { public string solution(string phone_number) { char[] nums = phone_number.ToCharArray(); for (int i = 0; i < nums.Length - 4; i++) { nums[i] = '*'; } return new string(nums); } }
없는 숫자 더하기
0부터 9까지의 숫자 중 일부가 들어있는 정수 배열 numbers가 매개변수로 주어집니다. numbers에서 찾을 수 없는 0부터 9까지의 숫자를 모두 찾아 더한 수를 return 하도록 solution 함수를 완성해주세요.
using System; using System.Linq; public class Solution { public int solution(int[] numbers) { int totalSum = 45; int numbersSum = numbers.Sum(); return totalSum - numbersSum; } }
제일 작은 수 제거하기
정수를 저장한 배열, arr 에서 가장 작은 수를 제거한 배열을 리턴하는 함수, solution을 완성해주세요. 단, 리턴하려는 배열이 빈 배열인 경우엔 배열에 -1을 채워 리턴하세요. 예를들어 arr이 [4,3,2,1]인 경우는 [4,3,2]를 리턴 하고, [10]면 [-1]을 리턴 합니다.
using System.Collections.Generic; using System.Linq; public class Solution { public int[] solution(int[] arr) { List<int> arrList = new List<int>(arr); int minValue = arrList.Min(); arrList.Remove(minValue); if (arrList.Count == 0) { arrList.Add(-1); } return arrList.ToArray(); } }
가운데 글자 가져오기
단어 s의 가운데 글자를 반환하는 함수, solution을 만들어 보세요. 단어의 길이가 짝수라면 가운데 두글자를 반환하면 됩니다.
public class Solution { public string solution(string s) { int length = s.Length; int middleIndex = length / 2; if (length % 2 == 0) { return s.Substring(middleIndex - 1, 2); } else { return s.Substring(middleIndex, 1); } } }
다음글이 없습니다.이전글이 없습니다.댓글