카테고리 없음

[코드업 기초 100제] 1053~1058 논리연산 문제

dev_zoe 2021. 1. 26. 22:24
반응형

문제

<1053>

1(true, 참) 또는 0(false, 거짓) 이 입력되었을 때
반대로 출력하는 프로그램을 작성해보자.

 

<1056>

두 가지의 참(1) 또는 거짓(0)이 입력될 때,
참/거짓이 서로 다를 때에만 참을 출력하는 프로그램을 작성해보자.

 

풀이

<1053>

Python

python에서는 1이 true, 0이 false이므로 not 연산자를 쓰면 값이 서로 바뀌게된다.

a = int(input())
print(not a)

 

JAVA

JAVA에서는 1이 true, 0이 false 값을 나타내지 않는다. (boolean 변수를 만들어서 사용해야함)

import java.io.*;
import java.util.*;

class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int a = Integer.parseInt(br.readLine());
    int b = a==0?1:0;

    System.out.print(b);
  }
}

 

<1056>

서로 다를 경우에만 참 -> not (a and b) => XOR

XOR 연산자는 ^

 

Python

a, b = map(int, input().split());
print(int(not(a==b)))
a, b = map(int, input().split());
print(int(a^b))

 

JAVA

import java.io.*;
import java.util.*;

class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());

    int a = Integer.parseInt(st.nextToken());
    int b = Integer.parseInt(st.nextToken());
    boolean c = a==1?true:false;
    boolean d = b==1?true:false;

    System.out.print(c^d?1:0);
  }
}

 

github.com/yurrrri/python_algorithm_practice/tree/main/codeup_basic100

 

yurrrri/python_algorithm_practice

(python) 알고리즘 문제풀이. Contribute to yurrrri/python_algorithm_practice development by creating an account on GitHub.

github.com

github.com/yurrrri/java_algorithm_practice/tree/main/codeup_basic100_java

 

yurrrri/java_algorithm_practice

(java) 알고리즘 문제풀이. Contribute to yurrrri/java_algorithm_practice development by creating an account on GitHub.

github.com

 

 

반응형