https://www.acmicpc.net/problem/5220
Error Detection
1 초 | 128 MB | 356 | 278 | 254 | 78.882% |
문제
In the midst of a fierce battle, Tony Stark’s suit constantly communicates with JARVIS for technical data. This data as transmitted takes the form of 16-bit integer values. However, due to various atmospheric issues (such as those created by all of that lightning Thor spreads around) there is some risk of data corruption. To help detect such corruption, for each 16-bit value that is transmitted, an additional single bit is sent. This additional single bit, called the check bit, is a 1 if the corresponding 16-bit integer has an ODD number of 1s when represented in binary. The check bit is a 0 if the corresponding 16-bit integer has an EVEN number of 1s when represented in binary. The effect is that: the number of bits set to 1 in the combined 17 bits is always EVEN.
For example, the integer 45 would appear in binary as 0000000000101101 which has an even number of 1s so the check bit would be 0. The integer 34173 would appear in binary as 1000010101111101 which has an odd number of 1s so the check bit would be 1.
입력
The first line in the test data file contains the number of test cases (< 100). After that, each line contains one test case: the first number is the 16-bit integer (provided as an int), and the following number is the check bit (also provided as an int).
출력
For each test case, you are to output “Corrupt” if the check bit doesn’t match up with the even or oddness of the integer, or “Valid” if it does.
출력 형식
정확한 출력 형식은 제출에서 언어를 Java로 설정하면 확인할 수 있다.
예제 입력 1 복사
4
34173 1
45 1
15 0
31 0
예제 출력 1 복사
Valid
Corrupt
Valid
Corrupt
풀이 : parity bit랑 비슷한 문제 같다.
입력을 이진수로 바꿨을 때 1의 개수가 홀수이면 1, 짝수이면 0이 되는데
입력으로 받은 t(0 또는 1)랑 같으면 Valid, 다르면 Corrupt 출력.
처음엔 입력 받은 수를 이진수로 바꾸는 부분을 직접 for 문을 통해 2로 나누고 그랬는데 시간 제한에서 걸림
그래서 Integer 클래스의 함수를 사용하여 10진수를 2진수로 변환하였다.
(Integer 클래스의 toBinaryString, toOctalString, toHexString 함수를 이용하면 각각 2진수, 8진수, 16진수로 변환할 수 있다.)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
// 백준 5220. Error Detection
private static boolean solveErrorDetection(int n, int p) {
boolean valid = false;
/* ------------------- INSERT CODE HERE ---------------------*/
int r = 0;
String binaryString = Integer.toBinaryString(n);
for(char c : binaryString.toCharArray()){
r += c -'0';
}
if((r%2==0 && p==0)||(r%2==1 && p==1)) {
valid = true;
}else {
valid = false;
}
/* -------------------- END OF INSERTION --------------------*/
return valid;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numCases = Integer.parseInt(br.readLine());
for(int i = 0; i < numCases; i++)
{
StringTokenizer st = new StringTokenizer(br.readLine());
int value = Integer.parseInt(st.nextToken());
int checkbit = Integer.parseInt(st.nextToken());
if (solveErrorDetection(value, checkbit)) {
System.out.println("Valid");
}
else {
System.out.println("Corrupt");
}
}
}
}
'알고리즘 문제풀이 > Beakjoon' 카테고리의 다른 글
백준 21312. 홀짝 칵테일 (JAVA) (0) | 2023.04.08 |
---|---|
백준 5768. Divide and Conquer (JAVA) (0) | 2023.03.31 |
백준 5086. 배수와 약수 (JAVA) (0) | 2023.03.20 |
백준 4619. 루트 (JAVA) (0) | 2023.03.19 |
백준 5691. 평균 중앙값 문제 (JAVA) (0) | 2023.03.16 |