본문 바로가기

기타35

[프로그래머스] Lv.0 숨어있는 숫자의 덧셈 (1) - 자바(java) 문제 설명 문자열 my_string이 매개변수로 주어집니다. my_string안의 모든 자연수들의 합을 return하도록 solution 함수를 완성해주세요. 내가 작성한 답 class Solution { public int solution(String my_string) { int answer = 0; my_string = my_string.replaceAll("[^0-9]", ""); String[] str = my_string.split(""); for(int i=0; i 2023. 10. 8.
[프로그래머스] Lv.0 a와 b 출력하기 - 자바(java) 문제 설명 정수 a와 b가 주어집니다. 각 수를 입력받아 입출력 예와 같은 형식으로 출력하는 코드를 작성해 보세요. 내가 작성한 답 import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); System.out.println("a = "+a); System.out.println("b = "+b); } } 2023. 9. 10.
[프로그래머스] Lv.0 n의 배수 - 자바(java) 문제 설명 정수 num과 n이 매개 변수로 주어질 때, num이 n의 배수이면 1을 return n의 배수가 아니라면 0을 return하도록 solution 함수를 완성해주세요. 내가 작성한 답 class Solution { public int solution(int num, int n) { int answer = 0; if(num%n == 0){ answer = 1; }else{ answer = 0; } return answer; } } 2023. 9. 1.
[프로그래머스] Lv.0 n 번째 원소까지 - 자바(java) 문제 설명 정수 리스트 num_list와 정수 n이 주어질 때, num_list의 첫 번째 원소부터 n 번째 원소까지의 모든 원소를 담은 리스트를 return하도록 solution 함수를 완성해주세요. 내가 작성한 답 class Solution { public int[] solution(int[] num_list, int n) { int[] answer = new int[n]; for(int i=0; i 2023. 8. 27.
[프로그래머스] Lv.0 카운트 업 - 자바(java) 문제 설명 정수 start_num와 end_num가 주어질 때, start_num부터 end_num까지의 숫자를 차례로 담은 리스트를 return하도록 solution 함수를 완성해주세요. 내가 작성한 답 import java.util.*; class Solution { public List solution(int start_num, int end_num) { List answer = new ArrayList(); for(int i=start_num; i 2023. 8. 26.
[프로그래머스] Lv.0 제곱수 판별하기 - 자바(java) 문제 설명 어떤 자연수를 제곱했을 때 나오는 정수를 제곱수라고 합니다. 정수 n이 매개변수로 주어질 때, n이 제곱수라면 1을 아니라면 2를 return하도록 solution 함수를 완성해주세요. 내가 작성한 답 class Solution { public int solution(int n) { int answer = 0; if(Math.sqrt(n)%1==0){ answer = 1; }else{ answer = 2; } return answer; } } Math.sqrt() 메서드는 자바의 java.lang.Math 클래스에 포함된 정적 메서드 중 하나로, 주어진 숫자의 제곱근을 반환하는 함수이다. 2023. 8. 25.
[프로그래머스] Lv.0 공배수 - 자바(java) 문제 설명 정수 number와 n, m이 주어집니다. number가 n의 배수이면서 m의 배수이면 1을 아니라면 0을 return하도록 solution 함수를 완성해주세요. 내가 작성한 답 class Solution { public int solution(int number, int n, int m) { int answer = (number%n==0) && (number%m==0) ? 1 : 0; return answer; } } 2023. 8. 25.
[프로그래머스] Lv.0 모음 제거 - 자바(java) 문제 설명 영어에선 a, e, i, o, u 다섯 가지 알파벳을 모음으로 분류합니다. 문자열 my_string이 매개변수로 주어질 때 모음을 제거한 문자열을 return하도록 solution 함수를 완성해주세요. 내가 작성한 답 class Solution { public String solution(String my_string) { String answer = my_string; String[] vowel = {"a", "e", "i", "o", "u"}; for (String v : vowel) { answer = answer.replaceAll(v, ""); } return answer; } } 2023. 8. 21.
[프로그래머스] Lv.0 자릿수 더하기 - 자바(java) 문제 설명 정수 n이 매개변수로 주어질 때 n의 각 자리 숫자의 합을 return하도록 solution 함수를 완성해주세요 내가 작성한 답 class Solution { public int solution(int n) { int answer = 0; while(n > 0){ answer += n % 10; n/=10; } return answer; } } 2023. 8. 14.
[프로그래머스] Lv.0 배열 원소의 길이 - 자바(java) 문제 설명 문자열 배열 strlist가 매개변수로 주어집니다. strlist 각 원소의 길이를 담은 배열을 retrun하도록 solution 함수를 완성해주세요. 내가 작성한 답 class Solution { public int[] solution(String[] strlist) { int[] answer = new int[strlist.length]; for(int i=0; i 2023. 8. 14.
[프로그래머스] Lv.0 문자 반복 출력하기 - 자바(java) 문제 설명 문자열 my_string과 정수 n이 매개변수로 주어질 때, my_string에 들어있는 각 문자를 n만큼 반복한 문자열을 return 하도록 solution 함수를 완성해보세요. 내가 작성한 답 class Solution { public String solution(String my_string, int n) { String answer = ""; for(int i=0; i 2023. 8. 14.
[프로그래머스] Lv.0 최대값 만들기(1) - 자바(java) 문제 설명 정수 배열 numbers가 매개변수로 주어집니다. numbers의 원소 중 두 개를 곱해 만들 수 있는 최댓값을 return하도록 solution 함수를 완성해주세요. 내가 작성한 답 class Solution { public int solution(int[] numbers) { int answer = 0; for(int i=0; i 2023. 8. 14.