728x90
반응형
- 프로그래머스 Level 1 : 최대공약수와 최소공배수2020-07-01 21:33:52public class Solution { public int[] solution(int n, int m) { int[] answer = new int[2]; //answer 배열에 2개가 담겨있다. int max = m; // 최댓값에 해당하는 부분을 max로 하여 m을 저장 int min = n; // 최솟값에 해당하는 부분을 min로 하여 n을 저장 int a = min; // a에 최솟 값을 저장 while (true) { a = max % min; //max와 min을 나눈 나머지를 a에 저장 max = min; //max에 min을 저장 if(a == 0) //나머지가 0이라면 { break; //중지 } min = a; //나머지는 최대공약수가 된다. } answer = new int[] {..
- 프로그래머스 Level 1 : 짝수와 홀수2020-06-29 20:25:36public class Solution { public string solution(int num) { string answer = ""; if (num % 2 == 0) { answer = "Even"; } else answer = "Odd"; return answer; } }
- 프로그래머스 Level1 : 평균 구하기2020-06-29 20:16:24public class Solution { public double solution(int[] arr) { double answer = 0; int a = 0; for(int i = 0; i
- 프로그래머스 Level 1 : 나누어 떨어지는 숫자 배열2020-06-29 20:07:35using System; using System.Collections.Generic; public class Solution { public int[] solution(int[] arr, int divisor) { int[] answer = new int[]{ }; int cnt = 0; //카운트 지정 List list = new List(); //사이즈 지정 없이 값을 추가 가능 for (int i = 0; i < arr.Length; i++) //배열의 길이만큼 반복 { if (arr[i] % divisor == 0) //인덱스 i를 divisor로 나누어 떨어질 때 { list.Add(arr[i]); //i를 추가한다. cnt++; // 카운트 1증가 } } if (cnt == 0) // 카운트가..
- 프로그래머스 Level 1 : 문자열 내림차순으로 배치하기2020-06-28 21:25:13public class Solution { public string solution(string s) { string answer = ""; char[] a = s.ToCharArray(); //string s 를 char배열로 변환 Array.Sort(a); // a를 정렬 Array.Reverse(a); //a를 역순으로 answer = new string(a); //char a를 다시 string으로 변한 return answer; } }
- 프로그래머스 Level1 : 문자열 다루기 기본2020-06-28 20:49:29public class Solution { public bool solution(string s) { bool answer = true; int resultcheck = 0;//결과를 체크하는 함수 선언 if (s.Length == 4 || s.Length == 6) { answer = int.TryParse(s, out resultcheck); //문자열을 정수형으로 표현하 } else { answer = false; } return answer; } } TryParse ( string , 함수 ) 숫자의 문자열 표현을 해당하는 32비트 부호 있는 정수로 변환합니다. 반환 값은 변환의 성공 여부를 나타냅니다. s : 변환할 숫자가 포함된 문자열입니다. resultcheck : 이 메서드는 변환이 성공한..
- 프로그래머스 Level1 : 자연수 뒤집어 배열로 만들기2020-06-28 18:50:39using System; public class Solution { public int[] solution(long n) { int[] answer = new int[] {}; string num = n.ToString(); //long n을 string으로 변환 char[] num1 = new char[num.Length]; //각 배열의 요소를 지정할 수 있는 char 선언 answer = new int[num1.Length]; //answer에 입력된 수의 길이 만큼 저장 for(int i=0; i
- 프로그래머스 Level1 : 행렬의 덧셈2020-06-28 18:30:07public class Solution { public int[,] solution(int[,] arr1, int[,] arr2) { int[,] answer = new int[arr1.GetLength(0),arr1.GetLength(1)]; // GetLength --> Array 안에 요소의 수를 뜻한다. // 이차원 배열은 Length가 아닌 GetLength를 사용하여야 한다. for (int i = 0; i < arr1.GetLength(0); i++) //행의 범위 , 0 : 행 { for (int j = 0; j < arr1.GetLength(1); j++) // 열의 범위, 1 : 열 { answer[i,j] = arr1[i, j] + arr2[i, j]; // arr1 과 arr2의 합..
- 프로그래머스 Level1 : 정수 내림차순으로 배치하기2020-06-28 17:52:45[ 얍삽한 방법 1 ] using System;//Array.sort를 사용하기 때문에 입력 public class Solution { public long solution(long n) { long answer = 0; string num = n.ToString();//long n을 string으로 변환 char[] num1 = new char[num.Length]; //string은 읽기 전용이다. 그래서 char배열을 선언 for (int i = 0; i < num1.Length; i++) //입력된 길이만큼 비교할 수 있게 반복문 작성 { num1[i] = num[i]; //string에 각 index번째있는 char들을 num1배열에 넣어준다 } Array.Sort(num1); //num1 정렬..
- 프로그래머스 Level1 : 자릿수 더하기2020-06-28 17:10:57using System; public class Solution { public int solution(int n) { int answer = 0; string num = n.ToString(); //int --> string으로 변환 for(int i=0; i < num.Length; i++) //입력된 수의 길이 만큼 반복 { answer += int.Parse(num[i].ToString());//string에 있는 char들을 다시 //string으로변환 후 int로 변환 }//num[0]에 '1'이 num[1]에 '2'
- 프로그래머스 Level 1 : 두 정수 사이의 합2020-06-25 20:16:30public class Solution { public long solution(int a, int b) { long answer = 0; if (a < b) { for (int i = a; i =b; i--) { answer += i; } } return answer; } } a와 b의 범위는 -10,000,000에서 +10,000,000까지이다. 1. a의 입력 값이 b의 입력 값보다 작을 때와 a의 입력 값이 b의 입력 값보다 클 때를 지정해줍니다. 2. 그리고 각각 내부에 a부터 b까지 반복하는 함수와 해당 값을 answer에 저장합니다. 3. a가 b보다 작을 때는 a부터 b까지 증가하는 반복문을 4. a가 b보다 작을 때는 a부터 b까지 감소하는 반복문을 선언해주어 문제를 해결합니다.
- 프로그래머스 LEVEL 1 : 직사각형 별찍기2020-06-25 20:02:03using System; public class Example { public static void Main() { String[] s; Console.Clear(); s = Console.ReadLine().Split(' '); int a = Int32.Parse(s[0]); int b = Int32.Parse(s[1]); for(int i = 0; i
728x90
반응형