본문 바로가기

프로그래머스 문제풀이15

프로그래머스 Level 1 : 최대공약수와 최소공배수 public 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[] {.. 2020. 7. 1.
프로그래머스 Level 1 : 짝수와 홀수 public class Solution { public string solution(int num) { string answer = ""; if (num % 2 == 0) { answer = "Even"; } else answer = "Odd"; return answer; } } 2020. 6. 29.
프로그래머스 Level1 : 평균 구하기 public class Solution { public double solution(int[] arr) { double answer = 0; int a = 0; for(int i = 0; i 2020. 6. 29.
프로그래머스 Level 1 : 나누어 떨어지는 숫자 배열 using 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) // 카운트가.. 2020. 6. 29.
프로그래머스 Level 1 : 문자열 내림차순으로 배치하기 public 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; } } 2020. 6. 28.
프로그래머스 Level1 : 문자열 다루기 기본 public 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 : 이 메서드는 변환이 성공한.. 2020. 6. 28.
프로그래머스 Level1 : 자연수 뒤집어 배열로 만들기 using 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 2020. 6. 28.
프로그래머스 Level1 : 행렬의 덧셈 public 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의 합.. 2020. 6. 28.
프로그래머스 Level1 : 정수 내림차순으로 배치하기 [ 얍삽한 방법 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 정렬.. 2020. 6. 28.
프로그래머스 Level1 : 자릿수 더하기 using 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' 2020. 6. 28.
반응형