목록알고리즘 (98)
forest_moon
짝수는 싫어요 문제 설명 정수 n이 매개변수로 주어질 때, n 이하의 홀수가 오름차순으로 담긴 배열을 return하도록 solution 함수를 완성해주세요. 제한사항 1 ≤ n ≤ 100 입출력 예 설명 입출력 #1 10 이하의 홀수가 담긴 배열 [1, 3, 5, 7, 9]를 return합니다. 입출력 #1 15 이하의 홀수가 담긴 배열 [1, 3, 5, 7, 9, 11, 13, 15]를 return합니다. class Solution { public int[] solution(int n) { int[] answer = {}; int a = 0; if(n % 2 == 0){ answer = new int[n / 2]; } else { answer = new int[n / 2 + 1 ]; } for(int ..
문제 설명 첫 번째 분수의 분자와 분모를 뜻하는 numer1, denom1, 두 번째 분수의 분자와 분모를 뜻하는 numer2, denom2가 매개변수로 주어집니다. 두 분수를 더한 값을 기약 분수로 나타냈을 때 분자와 분모를 순서대로 담은 배열을 return 하도록 solution 함수를 완성해보세요. 제한사항 0
문제 설명 머쓱이는 학교에서 키 순으로 줄을 설 때 몇 번째로 서야 하는지 궁금해졌습니다. 머쓱이네 반 친구들의 키가 담긴 정수 배열 array와 머쓱이의 키 height가 매개변수로 주어질 때, 머쓱이보다 키 큰 사람 수를 return 하도록 solution 함수를 완성해보세요. 제한사항 1 ≤ array의 길이 ≤ 100 1 ≤ height ≤ 200 1 ≤ array의 원소 ≤ 200 입출력 예 설명 입출력 예 #1 149, 180, 192, 170 중 머쓱이보다 키가 큰 사람은 180, 192, 170으로 세 명입니다. 입출력 예 #2 180, 120, 140 중 190보다 큰 수는 없으므로 0명입니다. class Solution { public int solution(int[] array, in..
두 수의 나눗셈 문제 설명 정수 num1과 num2가 매개변수로 주어질 때, num1을 num2로 나눈 값에 1,000을 곱한 후 정수 부분을 return 하도록 soltuion 함수를 완성해주세요. 제한사항 0
두 수의 합 문제 설명 정수 num1과 num2가 주어질 때, num1과 num2의 합을 return하도록 soltuion 함수를 완성해주세요. 제한사항 -50,000 ≤ num1 ≤ 50,000 -50,000 ≤ num2 ≤ 50,000 입출력 예 설명 입출력 예 #1 num1이 2이고 num2가 3이므로 2 + 3 = 5를 return합니다. 입출력 예 #2 num1이 100이고 num2가 2이므로 100 + 2 = 102를 return합니다. class Solution { public int solution(int num1, int num2) { answer = num1 + num2 ; return answer; } }
문제 설명 정수 n이 매개변수로 주어질 때 n의 각 자리 숫자의 합을 return하도록 solution 함수를 완성해주세요 제한사항 0 ≤ n ≤ 1,000,000 입출력 예 설명 입출력 예 #1 1 + 2 + 3 + 4 = 10을 return합니다. 입출력 예 #2 9 + 3 + 0 + 2 + 1 + 1 = 16을 return합니다. -- class Solution { public int solution(int n) { int answer = 0; while(n > 0){ answer += n%10; n/=10; } return answer; } } 10을 나눠주면서 한자리씩 소거.
문제 설명 my_string은 "3 + 5"처럼 문자열로 된 수식입니다. 문자열 my_string이 매개변수로 주어질 때, 수식을 계산한 값을 return 하는 solution 함수를 완성해주세요. 제한사항 연산자는 +, -만 존재합니다. 문자열의 시작과 끝에는 공백이 없습니다. 0으로 시작하는 숫자는 주어지지 않습니다. 잘못된 수식은 주어지지 않습니다. 5 ≤ my_string의 길이 ≤ 100 my_string을 계산한 결과값은 1 이상 100,000 이하입니다. my_string의 중간 계산 값은 -100,000 이상 100,000 이하입니다. 계산에 사용하는 숫자는 1 이상 20,000 이하인 자연수입니다. my_string에는 연산자가 적어도 하나 포함되어 있습니다. return type 은 정..
문제 설명 정수 배열 array가 매개변수로 주어질 때, 가장 큰 수와 그 수의 인덱스를 담은 배열을 return 하도록 solution 함수를 완성해보세요. 제한사항 1 ≤ array의 길이 ≤ 100 0 ≤ array 원소 ≤ 1,000 array에 중복된 숫자는 없습니다. 입출력 예 설명 입출력 예 #1 1, 8, 3 중 가장 큰 수는 8이고 인덱스 1에 있습니다. 입출력 예 #2 9, 10, 11, 8 중 가장 큰 수는 11이고 인덱스 2에 있습니다. class Solution { public int[] solution(int[] array) { int[] answer = new int[2]; int max = 0; int index = 0; for(int i=0; i max){ max = arr..