'Hammingcode'에 해당되는 글 1건

  1. 2021.12.30[HAMMINGCODE] Hamming Code

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

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

 

algospot.com :: HAMMINGCODE

Hamming Code 문제 정보 문제 Jaeha is writing an operating system for his toys, so they will be able to communicate with each other. However, the wireless chips in his toys are very cheap, so they are prone to transmission errors. Quite frequently, Ja

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
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        int[] bits = new int[7];
        Scanner scanner = new Scanner(System.in);
        int cases = scanner.nextInt();
        while (cases-- > 0) {
            char[] input = scanner.next().toCharArray();
            for (int i = 0; i < 7++i) {
                bits[i] = input[i] - '0';
            }
 
            int check1 = getXor(bits[0], bits[2], bits[4], bits[6]);
            int check2 = getXor(bits[1], bits[2], bits[5], bits[6]);
            int check3 = getXor(bits[3], bits[4], bits[5], bits[6]);
            int syndrome = 4 * check3 + 2 * check2 + check1;
            if (syndrome != 0) {
                bits[syndrome - 1= (bits[syndrome - 1+ 1) % 2;
            }
 
            System.out.printf("%d%d%d%d\n", bits[2], bits[4], bits[5], bits[6]);
        }
    }
 
    private static int getXor(int... bits) {
        int ret = bits[0];
        for (int i = 1; i < bits.length++i) {
            ret ^= bits[i];
        }
 
        return ret;
    }
}
cs



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

반응형

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

[WEEKLYCALENDAR] Weekly Calendar  (0) 2022.01.03
[CLOCKSYNC] Synchronizing Clocks  (0) 2022.01.02
[WEIRD] Weird Numbers  (0) 2021.12.30
[XHAENEUNG] 째능 교육  (0) 2021.12.29
[URI] URI Decoding  (0) 2021.12.29
Posted by N'