System.out.println((intMath.floor(4.4497083717E10));
System.out.println((long) Math.floor(4.4497083717E10));
System.out.println((long) 4.4497083717E10);
 
2147483647   -- due to an overflow
44497083717
44497083717



= 21413134219
= 19241313142
 
sum = (long) (Double.parseDouble(a) + Double.parseDouble(b));
 
sum = 40654447361



참고사이트

1. Floor function to float and double values

2. Formatting troubles: How to power to number?



'알고리즘 > 기초정리' 카테고리의 다른 글

완전 탐색 알고리즘 (최근접 점쌍 문제)  (0) 2016.10.27

영어 알파벳의 암호화 ( 예를 들어 connect의 경우 )


1. 영어 단어의 각각의 영어 알파벳을 그에 대응하는 번호로 변환한다. ( a는 0, b는 1, .... , z는 25가 된다 )

    connect → 2/14/13/13/4/2/19


2. 영어 단어의 알파벳을 거꾸로 뒤집는다.

    tcennoc → 19/2/4/13/13/14/2


3. 변환된 두 수를 더한다.

    21413134219 + 19241313142 = 40654447361


4. 더한 결과값을 다시 알파벳으로 변환한다. 이 때, 왼쪽에서 두 자리씩 끊어서 26으로 나눈 나머지 값에 대응하는 알파벳으로 변환한다.

    마지막 수가 한자리 수가 나온다면 바로 대응하는 알파벳으로 변환한다.

    40/65/44/47/36/1 → 14/13/18/21/10/1 → onsvkb



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
import java.util.Scanner;
 
public class Test3 {
    public static void main(String args[]){
        String english[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
                "k", "l", "m", "n", "o", "p", "q", "r", "s",
                "t", "u", "v", "w", "x", "y", "z"};
         
        Scanner sc = new Scanner(System.in);
        String num = sc.nextLine();
        String arr[] = num.split("");
         
        Long sum = (long) 0;
        String a="";
        String b="";
        for(int i=0; i<arr.length; i++){
            for(int j=0; j<english.length; j++){
                if(arr[i].equals(english[j])){
                    a = a+j;
                }
            }
        }
         
        for(int i=arr.length-1; i>=0; i--){
            for(int j=0; j<english.length; j++){
                if(arr[i].equals(english[j])){
                    b = b+j;
                }
            }
        }
        System.out.println("String = "+a);
        System.out.println("String2 = "+b);
        sum = (long) (Double.parseDouble(a) + Double.parseDouble(b));
        System.out.println("sum = "+sum);
         
        String arr2[] = String.valueOf(sum).split("");
        String result[] = new String[arr2.length/2+1];
        int data_num = 0;
        int nanu = 0;
        for(int i=0; i<=arr2.length; i+=2){  //2자리씩 끊기 위해
            if(i+2 > arr2.length){
                result[data_num] = String.valueOf(sum).substring(i);
            }else{
                result[data_num] = String.valueOf(sum).substring(i, i+2);
                nanu = Integer.parseInt(result[data_num]);
                if(nanu > 26){
                    nanu = nanu % 26;
                    result[data_num] = String.valueOf(nanu);
                }
            }
             
            data_num++;
        }
         
        String s = "";
        int result2 = 0;
        String value = "";
        for(int i=0; i<result.length; i++){
            result2 = Integer.parseInt(result[i]);
            System.out.println("result2 = "+result2);   //숫자
 
            value = english[result2];
            s = s+value;
        }
        System.out.println(s);
    }
}


결과값

connect
String = 21413134219
String2 = 19241313142
sum = 40654447361
result2 = 14
result2 = 13
result2 = 18
result2 = 21
result2 = 10
result2 = 1
onsvkb



완전 제곱수 : 정수의 곱셈으로 된 수 (예를 들어 1의 제곱인 1, 2의 제곱인 4, 3의 제곱인 9 등이 있다)


주어진 수의 범위 내에서 완전 제곱수의 개수 구하기

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.Scanner;
 
public class Test2 {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        String num = sc.nextLine();
        String arr[] = num.split(" ");
         
        int a = Integer.parseInt(arr[0]);
        int b = Integer.parseInt(arr[1]);
        int value = 0;
         
        int first = 0;
        int result = 0;
         
        for(;;){
            first++;
            value = (int) Math.pow(first,2);
            if(value >= a && value <=b){
                result++;
            }
            if(value > b){
                break;    //제곱수가 최댓값보다 크면 for문을 빠져 나온다.
            }
        }
         
        System.out.println(result);
    }
}
<p></p>


결과값

500 2600
28


→ 500과 2600 사이의 완전제곱수의 갯수는 28개이다.

'알고리즘 > 응용문제' 카테고리의 다른 글

[문제 3] 영어 알파벳의 암호화  (0) 2016.12.20
[문제 1] 주어진 수의 평균 구하기  (0) 2016.12.19
입력 받은 모든 수들의 평균 구하기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;
 
public class Test {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();     //입력받을 수의 개수
         
        int arr[] = new int[num];
         
        for(int i=0; i<num; i++){
            arr[i] = sc.nextInt();  //평균을 구할 수
        }
         
        int sum = 0;
        for(int j=0; j<num; j++){
            sum = sum + arr[j];
        }
         
        System.out.println(sum/num);
    }
}


결과값

4
10
20
30
40
25


→ 4개의 값을 입력 받아 그 수들의 평균값 구하기

(10+20+30+40) / 4 = 25



최근접 점쌍 문제 (closest pair problem)

여러 개의 1차원의 점을 표준 입력에서 읽은 후, 그 중 가장 가까운 거리에 있는 두 점의 한 쌍을 출력하는 문제이다.

임의조건 : 입력되어지는 숫자들은 공백으로 구분되며, 각각 10자리 이하의 자연수이다.


완전 탐색 알고리즘 (Brute-force algorithm)

최근접 점쌍은 완전 탐색 알고리즘을 사용하면 O(n2)의 시간에 찾을 수 있다. 이를 위해서는 아래와 같이 개의 모든 쌍들 사이의 거리를 계산하여 그 중 가장 가까운 두 점을 고르면 된다.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
 
public class test {
    public static void main(String[] args) {
        ArrayList<Integer> numberList = new ArrayList<Integer>();
 
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            String input = br.readLine();
            String[] numStrList = input.split(" ");
            for (String numStr : numStrList) {
                numberList.add(Integer.parseInt(numStr));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        Collections.sort(numberList);       //정렬
        Collections.reverse(numberList);    //내림차순 정렬
 
        //완전 탐색 알고리즘
        int minDist = Integer.MAX_VALUE;
        int temp;
        for(int i=0; i<numberList.size()-1; i++){
            for(int j=i+1; j<numberList.size(); j++){
                temp = distance(numberList.get(i), numberList.get(j));
                if (temp < minDist) {
                    minDist = temp;
                    System.out.println("minDist = "+minDist);
                    System.out.println("result = "+numberList.get(i)+", "+numberList.get(j));
                }
                
            }
        }
    }
    
    public static int distance(int p, int q) {
        int dis = p-q;
        return dis;
    }
}
 
cs


결과값