'Java'에 해당되는 글 37건

  1. 2021.12.27[PICNIC] 소풍
  2. 2021.12.27[ENCRYPT] 문자열 암호화
  3. 2021.12.27[LECTURE] Lecture Note
  4. 2021.12.27[DRAWRECT] 사각형 그리기
  5. 2021.12.27[ENDIANS] Endians
  6. 2021.12.27[MERCY] Merciful Algospot
  7. 2021.12.27[HELLOWORLD] Hello World

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/PICNIC

 

algospot.com :: PICNIC

소풍 문제 정보 문제 안드로메다 유치원 익스프레스반에서는 다음 주에 율동공원으로 소풍을 갑니다. 원석 선생님은 소풍 때 학생들을 두 명씩 짝을 지어 행동하게 하려고 합니다. 그런데 서로

www.algospot.com

 

풀이입니다. 🤔

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
import java.util.*;
 
public class Main {
    // 친구 관계 정의
    // friends[0][1] 이 true 라면, 0 과 1 은 친구.
    private static final boolean[][] friends = new boolean[10][10];
    private static final boolean[] uses = new boolean[10];
 
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int cases = scanner.nextInt();
        while (cases-- > 0) {
            int n = scanner.nextInt(), m = scanner.nextInt();
            for (int i = 0; i < m; ++i) {
                int a = scanner.nextInt(), b = scanner.nextInt();
                friends[a][b] = true;
                friends[b][a] = true;
            }
            
            System.out.println(getCombinationCount(n));
            
            Arrays.fill(uses, false);
            for (int i = 0; i < n; ++i) Arrays.fill(friends[i], false);
        }
    }
 
    private static int getCombinationCount(int n) {
        int indexOfNotUses = getMinIndexOfNotUses(n);
        // 기저 사례 : 모든 사람이 사용되었음
        if (indexOfNotUses == -1return 1;
 
        // 작은 수가 큰 수를 선택하는 방식으로 탐색할 가짓수를 줄인다.
        // [1, 3] 과 [3, 1] 은 동일.
        int ret = 0;
        for (int i = indexOfNotUses + 1; i < n; ++i) {
            if (uses[i]) continue;
            if (!friends[indexOfNotUses][i]) continue;
 
            uses[indexOfNotUses] = uses[i] = true;
            ret += getCombinationCount(n);
            uses[indexOfNotUses] = uses[i] = false;
        }
 
        return ret;
    }
 
    private static int getMinIndexOfNotUses(int n) {
        for (int i = 0; i < n; ++i) {
            if (!uses[i]) return i;
        }
 
        return -1;
    }
}
 
cs

 


이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[HOTSUMMER] 에어컨을 끈다고 전력난이 해결될까?  (0) 2021.12.27
[CONVERT] Conversions  (0) 2021.12.27
[ENCRYPT] 문자열 암호화  (0) 2021.12.27
[LECTURE] Lecture Note  (0) 2021.12.27
[DRAWRECT] 사각형 그리기  (0) 2021.12.27
Posted by N'

Java로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 Java1.8부터 제공되는 함수형 API는 사용하지 않습니다.


문제 : https://www.algospot.com/judge/problem/read/ENCRYPT

 

algospot.com :: ENCRYPT

문자열 암호화 문제 정보 문제 특정 메시지를 암호화 하는 방법은 오랫 동안 다양하게 연구되었다. 그러한 방법들 중에서 가장 간단한 방법을 생각해보자. 특정 문자열을 입력받는다. 편의상 문

www.algospot.com


풀이입니다. 🤔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int cases = scanner.nextInt();
        while (cases-- > 0) {
            String str = scanner.next();
            int len = str.length();
            for (int i = 0; i < len; i += 2System.out.print(str.charAt(i));
            for (int i = 1; i < len; i += 2System.out.print(str.charAt(i));
            System.out.println();
        }
    }
}
cs

 

 

이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[CONVERT] Conversions  (0) 2021.12.27
[PICNIC] 소풍  (0) 2021.12.27
[LECTURE] Lecture Note  (0) 2021.12.27
[DRAWRECT] 사각형 그리기  (0) 2021.12.27
[ENDIANS] Endians  (0) 2021.12.27
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/LECTURE

 

algospot.com :: LECTURE

Lecture Note 문제 정보 문제 Professor Lew teaches Algorithm course in Sonyusi University (소녀시대학교). It is his first year as a professor, so he spends a lot of time making lecture notes. He'll teach recursion and sorting algorithms in the n

www.algospot.com

 

풀이입니다. 🤔

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
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        List<String> strings = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        int cases = scanner.nextInt();
        while (cases-- > 0) {
            String line = scanner.next();
 
            for (int i = 0, len = line.length(); i < len; i += 2) strings.add(line.substring(i, i + 2));
            sort(strings);
 
            for (String str : strings) System.out.print(str);
            System.out.println();
 
            strings.clear();
        }
    }
 
    // Java 표준 Sort 를 사용해도 좋다.
    private static void sort(List<String> strings) {
        for (int i = 0, size = strings.size(); i < size; ++i) {
            for (int j = i + 1; j < size; ++j) {
                String a = strings.get(i), b =  strings.get(j);
                if (a.compareTo(b) > 0) {
                    strings.set(i, b);
                    strings.set(j, a);
                }
            }
        }
    }
}
cs

 


이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[PICNIC] 소풍  (0) 2021.12.27
[ENCRYPT] 문자열 암호화  (0) 2021.12.27
[DRAWRECT] 사각형 그리기  (0) 2021.12.27
[ENDIANS] Endians  (0) 2021.12.27
[MERCY] Merciful Algospot  (0) 2021.12.27
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/DRAWRECT

 

algospot.com :: DRAWRECT

사각형 그리기 문제 정보 문제 AdbyMe, Inc. 의 인턴인 A.I.는 웹 브라우저에 직사각형을 그리는 코드를 작성해야 한다. 웹 브라우저는 직사각형 모양의 뷰포트를 가지고 있고, 그려지는 직사각형의

www.algospot.com


풀이
입니다. 🤔

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
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Map<Integer, Integer> xCounts = new HashMap<>(), yCounts = new HashMap<>();
        Scanner scanner = new Scanner(System.in);
        int cases = scanner.nextInt();
        while (cases-- > 0) {
            for (int i = 0; i < 3++i) {
                input(xCounts, scanner.nextInt());
                input(yCounts, scanner.nextInt());
            }
 
            System.out.printf("%d %d\n", getAnswer(xCounts), getAnswer(yCounts));
 
            xCounts.clear();
            yCounts.clear();
        }
    }
 
    private static void input(Map<Integer, Integer> counts, int num) {
        if (counts.containsKey(num)) counts.put(num, counts.get(num) + 1);
        else counts.put(num, 1);
    }
 
    private static int getAnswer(Map<Integer, Integer> countGroup) {
        Set<Integer> keySet = countGroup.keySet();
        if (countGroup.size() == 1return keySet.iterator().next();
        else {
            for (Integer n : keySet) {
                if (countGroup.get(n) == 1return n;
            }
        }
 
        throw new IllegalStateException("Cannot found answer");
    }
}
 
cs

 


이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[ENCRYPT] 문자열 암호화  (0) 2021.12.27
[LECTURE] Lecture Note  (0) 2021.12.27
[ENDIANS] Endians  (0) 2021.12.27
[MERCY] Merciful Algospot  (0) 2021.12.27
[HELLOWORLD] Hello World  (0) 2021.12.27
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.


문제 : https://www.algospot.com/judge/problem/read/ENDIANS

 

algospot.com :: ENDIANS

Endians 문제 정보 문제 The two island nations Lilliput and Blefuscu are severe rivals. They dislike each other a lot, and the most important reason was that they break open boiled eggs in different ways. People from Lilliput are called little-endians

www.algospot.com


풀이입니다. 🤔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int cases = scanner.nextInt();
        while (cases-- > 0) {
            long n = scanner.nextLong(); // Input is 32-bit unsigned
            // 00010010[1] 00110100[2] 01010110[3] 01111000[4]
            System.out.println(
                    ((n & 0xff<< 24+        // Parse [4] and shift left by 24bit
                    ((n & 0xff00<< 8+       // Parse [3] and shift left by 8bit
                    ((n & 0xff0000>> 8+     // Parse [2] and shift right by 8bit
                    ((n & 0xff000000>> 24)    // Parse [2] and shift right by 24bit
            );
        }
    }
}
cs



이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

 

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[ENCRYPT] 문자열 암호화  (0) 2021.12.27
[LECTURE] Lecture Note  (0) 2021.12.27
[DRAWRECT] 사각형 그리기  (0) 2021.12.27
[MERCY] Merciful Algospot  (0) 2021.12.27
[HELLOWORLD] Hello World  (0) 2021.12.27
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/MERCY

 

algospot.com :: MERCY

Merciful Algospot 문제 정보 문제 The administrators of algospot.com are so merciful, that they prepared really, really easy problem to prevent contestants from frustration. 입력 Input contains just one positive integer N(N <= 10). 출력 Print N li

www.algospot.com


풀이입니다. 🤔

1
2
3
4
5
6
7
8
9
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int cases = scanner.nextInt();
        while (cases-- > 0System.out.println("Hello Algospot!");
    }
}
cs

 


이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[ENCRYPT] 문자열 암호화  (0) 2021.12.27
[LECTURE] Lecture Note  (0) 2021.12.27
[DRAWRECT] 사각형 그리기  (0) 2021.12.27
[ENDIANS] Endians  (0) 2021.12.27
[HELLOWORLD] Hello World  (0) 2021.12.27
Posted by N'

Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/HELLOWORLD

 

algospot.com :: HELLOWORLD

Hello World! 문제 정보 문제 예의 바른 프로그래머들은 인사를 잘 합니다. 프로그래밍 언어를 배우면서 처음으로 짜는 프로그램이 항상 Hello World! 인 것만 봐도 알 수 있지요. AOJ 의 첫 문제에서도

www.algospot.com


풀이입니다. 🤔

1
2
3
4
5
6
7
8
9
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int cases = scanner.nextInt();
        while (cases-- > 0System.out.printf("Hello, %s!\n", scanner.next());
    }
}
cs

 

 


이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[ENCRYPT] 문자열 암호화  (0) 2021.12.27
[LECTURE] Lecture Note  (0) 2021.12.27
[DRAWRECT] 사각형 그리기  (0) 2021.12.27
[ENDIANS] Endians  (0) 2021.12.27
[MERCY] Merciful Algospot  (0) 2021.12.27
Posted by N'