문방구앞오락기
2017. 8. 31. 10:24
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | //A perfect power is a classification of positive integers: // //In mathematics, a perfect power is a positive integer that can be expressed as an integer power of another positive integer. More formally, n is a perfect power if there exist natural numbers m > 1, and k > 1 such that mk = n. //Your task is to check wheter a given integer is a perfect power. If it is a perfect power, return a pair m and k with mk = n as a proof. Otherwise return Nothing, Nil, null, None or your language's equivalent. // //Note: For a perfect power, there might be several pairs. For example 81 = 3^4 = 9^2, so (3,4) and (9,2) are valid solutions. However, the tests take care of this, so if a number is a perfect power, return any pair that proves it. // //Examples // //isPerfectPower(4) => new int[]{2,2} //isPerfectPower(5) => null //isPerfectPower(8) => new int[]{2,3} //isPerfectPower(9) => new int[]{3,2} package Suss; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; public class CodrWar_7 { //best public static int[] isPerfectPower(int n) { for (int i = 2; ; i++) { int root = (int) Math.round(Math.pow(n, 1.0 / i)); if (root < 2) return null; if (Math.pow(root, i) == n) return new int[]{root, i}; } } public static int[] isPerfectPower_1(int n) { int process = 0; int count = 0; int r1=0; int r2=0; int[] out = new int[2]; for(int i = 2 ; i <=Math.sqrt(n) - 1 ; i++){ //Math.sqrt(n)을 한이유는 반복문 횟수를 줄이기 위해다 //왜냐하면 해당 제곱근값 안에 모든 약수가 들어오기 때문이다 // 예를들어 100의 약수는 1 2 5 10 25 50 100이다. // 10 25 50 100은 1 2 5 10의 약수이다. // 따라서 100의 sqrt 10의 약수에 모든 약수가 들어온다. // 에라토스테네스의 체 확인 // System.out.println(i+" // "+n%i); if(n%i == 0 ){ process = n; while(true){ System.out.println(process+ " and "+ i); r1 = process%i; System.out.println(r1); // System.out.println(process%i); r2 = process/i; // System.out.println(process/i); System.out.println(r2); if(process%i !=0 ){ System.out.println("여기"); // 값은 0이면서 // 나머지 1일때 System.out.println(r1); System.out.println(r2); if(r1 == 1 && r2 == 0 ){ System.out.println("이포인트 " +count ); if(count > 1){ out[0] = i; out[1] = count; System.out.println("아웃"); map.put(i, count); } } break; } count++; process = process/i; } count=0; } } System.out.println(map.toString()); System.out.println(Arrays.toString(out)); if(out[0] + out[1] <= 0){ return null; } return out; } public static void main(String[] args) { System.out.println(Arrays.toString(isPerfectPower(81))); } } |