프로그래머스 - 최고의 집합

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 import java.util.Arrays; //테스트로 출력해 보기 위한 코드입니다. public class BestSet { public int[] bestSet(int n, int s){ int[] answer = null; if(n>s) { answer = new int[1]; answer[0]=-1; return answer; } answer = new int[n]; int a = s/n; for(int i=0;i<n;i++) { answer[i]=a; } for(int i=0;i<s%n;i++) { answer[n-1-i]++; } return answer; } public static void main(String[] args) { BestSet c = new BestSet(); //아래는 테스트로 출력해 보기 위한 코드입니다. System.out.println(Arrays.toString(c.bestSet(3,13))); } }

12월 23, 2017 · Jaejin Jang

프로그래머스 - 최대값과 최소값

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 import java.util.Vector; public class GetMinMaxString { public String getMinMaxString(String str) { Integer min=Integer.MAX_VALUE,max=Integer.MIN_VALUE; String[] sepstr = str.split(" "); Vector<Integer> vec = new Vector<Integer>(); for(int i=0;i<sepstr.length;i++) { vec.addElement(Integer.parseInt(sepstr[i])); } for(int i=0;i<vec.size();i++) { if(vec.get(i)<min) { min = vec.get(i); } if(vec.get(i) > max) { max = vec.get(i); } } return min+" "+max; } public static void main(String[] args) { String str = "1 2 3 4"; GetMinMaxString minMax = new GetMinMaxString(); //아래는 테스트로 출력해 보기 위한 코드입니다. System.out.println("최대값과 최소값은?" + minMax.getMinMaxString(str)); } } 아래와 같은 풀이가 있다. 좋은 방법인듯 ...

12월 23, 2017 · Jaejin Jang

프로그래머스 - 최대공약수와 최소공배수

유클리트 호제법으로 gcd를 구하고, gcd를 이용해 lcm을 구했다. 포인트는 gcd를 이용해 lcm을 구하는 것이다. 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 import java.util.Arrays; class TryHelloWorld { public int gcd(int a,int b) { if(a==0){ return b; } else { return gcd(b%a,a); } } public int[] gcdlcm(int a, int b) { int gcd = gcd(a,b); int lcm = a*b/gcd; int[] answer = new int[2]; answer[0] = gcd; answer[1] = lcm; return answer; } // 아래는 테스트로 출력해 보기 위한 코드입니다. public static void main(String[] args) { TryHelloWorld c = new TryHelloWorld(); System.out.println(Arrays.toString(c.gcdlcm(3, 12))); } }

12월 23, 2017 · Jaejin Jang

프로그래머스 - 최솟값 만들기

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 import java.util.Arrays; public class TryHelloWorld { public int getMinSum(int []A, int []B) { int answer = 0; Arrays.sort(A); Arrays.sort(B); for(int i=0;i<B.length;i++) { answer += A[i]*B[B.length-1-i]; } return answer; } public static void main(String[] args) { TryHelloWorld test = new TryHelloWorld(); int []A = {1,2}; int []B = {3,4}; System.out.println(test.getMinSum(A,B)); } }

12월 23, 2017 · Jaejin Jang

프로그래머스 - 콜라츠 추측

연산도중에 int형의 범위를 넘어갈 수 있기 때문에 long으로 바꿔주는 것이 포인트! 시간 많이 날림 ㅜ 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 class Collatz { public int collatz(long num) { int answer = 0; int i; for(i=0;i<500;i++) { if(num==1) return answer; if(num%2==0) num/=2; else num = (num*3)+1; answer++; } if(num==1) return answer; else return -1; } // 아래는 테스트로 출력해 보기 위한 코드입니다. public static void main(String[] args) { Collatz c = new Collatz(); int ex = 6; System.out.println(c.collatz(ex)); } }

12월 23, 2017 · Jaejin Jang

프로그래머스 - 평균구하기

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class GetMean { public int getMean(int[] array) { int sum=0; for(int i=0;i<array.length;i++) { sum+=array[i]; } return sum/array.length; } public static void main(String[] args) { int x[] = {5, 4, 3}; GetMean getMean = new GetMean(); // 아래는 테스트로 출력해 보기 위한 코드입니다. System.out.println("평균값 : " + getMean.getMean(x)); } } 아래와 같은 방법이 있는데 java8부터 추가된 기능이다. 하지만 수행시간의 차이가 40~50배가 났다. ...

12월 23, 2017 · Jaejin Jang

프로그래머스 - 피보나치 수

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class Fibonacci { public long fibonacci(int num) { if(num==0) { return 0; } else if(num==1){ return 1; } else{ return fibonacci(num-1)+fibonacci(num-2); } } // 아래는 테스트로 출력해 보기 위한 코드입니다. public static void main(String[] args) { Fibonacci c = new Fibonacci(); int testCase = 3; System.out.println(c.fibonacci(testCase)); } } 다음에는 dp로 풀어봐야겠다. ...

12월 23, 2017 · Jaejin Jang

프로그래머스 - 하샤드 수

나의 풀이 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 import java.util.Arrays; public class TryHelloWorld { public int getMinSum(int []A, int []B) { int answer = 0; Arrays.sort(A); Arrays.sort(B); for(int i=0;i<B.length;i++) { answer += A[i]*B[B.length-1-i]; } return answer; } public static void main(String[] args) { TryHelloWorld test = new TryHelloWorld(); int []A = {1,2}; int []B = {3,4}; System.out.println(test.getMinSum(A,B)); } } 문자열로 형변환 없이 풀어낸 코드 ...

12월 23, 2017 · Jaejin Jang

프로그래머스 - 행렬의 곱셈

머리속에서는 쉽지만 막상 구현하면 헷갈려서 잘 틀리는 행렬곱… 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class ProductMatrix { public int[][] productMatrix(int[][] A, int[][] B) { if(A[0].length!=B.length) return null; int[][] answer = new int[A.length][B[0].length]; for(int i=0;i<A.length;i++) { for(int j=0;j<B[0].length;j++) { for(int k=0;k<B.length;k++) { answer[i][j]+=(A[i][k]*B[k][j]); } } } return answer; } public static void main(String[] args) { ProductMatrix c = new ProductMatrix(); int[][] a = { { 1, 2 }, { 2, 3 } }; int[][] b = { { 3, 4 }, { 5, 6 } }; // 아래는 테스트로 출력해 보기 위한 코드입니다. System.out.println("행렬의 곱셈 : " + c.productMatrix(a, b)); }

12월 23, 2017 · Jaejin Jang

프로그래머스 - 행렬의 덧셈

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 class SumMatrix { int[][] sumMatrix(int[][] A, int[][] B) { int[][] answer = new int[A.length][A[0].length]; for(int i=0;i<A.length;i++) { for(int j=0;j<A[0].length;j++) { answer[i][j] = A[i][j]+B[i][j]; } } return answer; } // 아래는 테스트로 출력해 보기 위한 코드입니다. public static void main(String[] args) { SumMatrix c = new SumMatrix(); int[][] A = { { 1, 2 }, { 2, 3 } }; int[][] B = { { 3, 4 }, { 5, 6 } }; int[][] answer = c.sumMatrix(A, B); if (answer[0][0] == 4 && answer[0][1] == 6 && answer[1][0] == 7 && answer[1][1] == 9) { System.out.println("맞았습니다. 제출을 눌러 보세요"); } else { System.out.println("틀렸습니다. 수정하는게 좋겠어요"); } } }

12월 23, 2017 · Jaejin Jang