본문 바로가기

COTE/programmers

(26)
[java] 콜라츠 추측 12345678910111213141516171819202122232425package www.gohome.com;//콜라츠추측 public class ColatzGuessing { public static int solution(int num) { long n = (long)num; for(int i=0; i3),1(3->10),2(10->5),3(5->16),4(16->8),5(8->4),6(4->2),7(2->1),8(1) if(n == 1) return i; if(n % 2 ==0) { n = n/2; }else { n = n*3 +1; } } return -1; } public static void main(String[]args) { ColatzGuessing sol = new ColatzGu..
[java] 평균 구하기 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package www.gohome.com; //평균 구하기 public class getAverage { public static double solution(int[]arr) { int sum = 0; for(int i=0; i
[java]하샤드 수 num % num의 자릿수 합 == 0 : true '각 자릿수' 합을 구하기 위해 char로 만든 뒤 array에 집어넣기 int num -> String num -> char[ ] num 순서로 넣고 for문에서 하나씩 꺼내 더한다 이때 char -> int 형태로 num을 바꿔주기 위해 Character.getNumericValue() 를 사용한다 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package www.gohome.com; import java.util.Arrays; //하샤드 수 public class HashadNumber { public static boolean so..
[java]핸드폰 번호 가리기 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package www.gohome.com; import java.util.Arrays; public class HideCellphoneNumber { public static String sol(String phone_number) { char[] arr = phone_number.toCharArray(); for(int i=0; i
[java] 행렬의 덧셈 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package www.gohome.com; import java.util.Arrays; public class AddicionOfMatrices{ public int[][] solution(int[][]arr1, int[][]arr2){ int[][] answer = new int[arr1.length][arr1[0].length]; for(int i=0; i
[java] x만큼 간격이 있는 n개의 숫자 풀이 처음엔 굳이 왜 num이라는 객체를 따로 만들어서 계산할까 했는데 for문에서 num대신 for(int i=0; i