Computer Science (CS)/알고리즘

Computer Science (CS)/알고리즘

[프로그래머스/Java] 문자열 내 p와 y의 개수

문제 정답 코드 class Solution { boolean solution(String s) { boolean answer = true; // int pCount = 0; int yCount = 0; String[] array = s.split(""); for(int i = 0; i

Computer Science (CS)/알고리즘

[프로그래머스/Java] 정수 제곱근 판별

문제 정답 코드 class Solution { public long solution(long n) { long answer = 0; // answer = -1; for(long i = 1; i*i

Computer Science (CS)/알고리즘

[프로그래머스/Java] 자연수 뒤집어 배열로 만들기

문제 정답 코드 class Solution { public int[] solution(long n) { int[] answer = {}; // int count = 0; long num = n; while(num != 0){ num /= 10; count++; } num = n; answer = new int[count]; for(int i = 0; num!=0; i++){ answer[i] = (int)(num%10); num /= 10; } // return answer; } } num변수를 설정해주지 않고 n을 사용하게 되면 while문 돌고 난 후 n=0이 되서 사용을 하지 못하기 때문에 초기화 용도로 num 변수를 만들어줬습니다. 이번에도 주의할 점은 n이 long형이라는 것! for문에서 an..

Computer Science (CS)/알고리즘

[프로그래머스/Java] x만큼 간격이 있는 n개의 숫자

문제 정답 코드 class Solution { public long[] solution(int x, int n) { long[] answer = {}; // answer = new long[n]; long result = x; for(int i = 0; i < n; i++){ answer[i] = result; result+=x; } // return answer; } } 처음에는 result를 생성안하고 x를 썼는데 x값이 반복될 때마다 바뀌니까 기댓값과 다르게 나오더라고요. 주의할 점이 answer 배열이 long형이라는 것! result 변수 설정할 때 int형으로 하면 테스트케이스 3개는 통과하지만 제출 시 실패로 나옵니다!

Computer Science (CS)/알고리즘

[프로그래머스/Java] 자릿수 더하기

문제 정답 코드 import java.util.*; public class Solution { public int solution(int n) { int answer = 0; // while(n > 0){ answer += n%10; n /= 10; } // return answer; } } 이제부터 주석 사이에 있는 코드들이 직접 작성한 코드라고 봐주시면 됩니다. 이 문제는 살짝 헤맸던(?) 문제입니다. 첫 시도는 아래와 같습니다. import java.util.*; public class Solution { public int solution(int n) { int answer = 0; // int i = 10; while(true){ if(n

Computer Science (CS)/알고리즘

[프로그래머스/Java] 짝수와 홀수

문제 정답 코드 class Solution { public String solution(int num) { String answer = ""; if(num % 2 == 0) answer = "Even"; else answer = "Odd"; return answer; } } 조건문만 직접 작성했습니다. 아직까지는 어렵다는 생각은 다행히도 안드네요. ㅎㅎ

Computer Science (CS)/알고리즘

[프로그래머스/Java] 평균 구하기

문제 정답 코드 class Solution { public double solution(int[] arr) { double answer = 0; for(int i = 0; i

Computer Science (CS)/알고리즘

[프로그래머스/Java] 약수의 합

문제 정답 코드 class Solution { public int solution(int n) { int answer = 0; for(int i = 1; i

Computer Science (CS)/알고리즘

[백준/Java] 1546번 평균

문제 정답 코드 import java.io.*; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); double[] array = new double[Integer.parseInt(br.readLine())]; StringTokenizer st = new StringTokenizer(br.readLine(), " "); for(int z=0; z< array.length; z++) { array[z] = Double.parse..

Computer Science (CS)/알고리즘

[백준/Java] 10811번 바구니 뒤집기

문제 정답 코드 import java.io.*; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine(), " "); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int[] array = new int[n]; for(int i=0; i

eune7
'Computer Science (CS)/알고리즘' 카테고리의 글 목록 (8 Page)