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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | public class CodingPractice01 { // 주어진 정수를 뒤집어서 출력 // Ex : 46312 => 21364 public static int getReversedInteger(int val){ StringBuffer returnVal = new StringBuffer(); int length = String.valueOf(val).length(); for(int i = 0 ; i < length ; i ++){ returnVal.append(val%10); val /= 10; } return Integer.parseInt(returnVal.toString()); } // 배열을 오름차순으로 정렬하고 중간값 출력 // 입력받은 수들의 개수가 짝수면 두 개의 중간값의 평균을 출력 // Ex : 10, 2, 101, 90 => 50 public static double getMedian(int[] val){ int indexMin, temp; // Selection sort for(int i = 0 ; i < val.length; i++){ indexMin = 0; for(int j = i; j < val.length ; j++){ if(val[indexMin] > val[j]){ indexMin = j; } } temp = val[indexMin]; val[indexMin] = val[i]; val[i] = temp; } if(val.length % 2==0){ return (val[val.length/2] + val[val.length/2+1])/2; }else{ return val[val.length/2+1]; } } // 해당 순번의 피보나치의 값 출력 // Ex : 2 => 1, 8 => 21 // no recursive public static int getFibonacciNumber(int val){ int prePre = 0; int pre = 1; int in = 0; if(val <= 0){ return prePre; }else if(val == 1){ return pre; }else{ for(int i = 0 ; i < val-1 ; i ++){ in = prePre + pre; prePre = pre; pre = in; } return pre; } } public static void main(String args[]){ System.out.println(getReversedInteger(46312)); // 21364 int arrayInt[] = {10, 2, 101, 90}; System.out.println(getMedian(arrayInt)); // 50.0 System.out.println(getFibonacciNumber(8)); // 21 } } | cs |
반응형
'잔잔바리 > CodingPractice' 카테고리의 다른 글
[Java/CodingPractice]04. CodingPractice04 (0) | 2016.08.01 |
---|---|
[Java/CodingPractice]03. CodingPractice03 (0) | 2016.07.30 |
[Java/CodingPractice]02. CodingPractice02 (0) | 2016.06.05 |