알고리즘
코드업 100제 (1056~1060)
double_R_one_G
2020. 9. 22. 11:07
728x90
1056. 두 가지의 참(1) 또는 거짓(0)이 입력될 때,
참/거짓이 서로 다를 때에만 참을 출력하는 프로그램을 작성해보자.
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
if(x==1 && y==1 || x==0 &&y==0 )
System.out.println("거짓");
else
System.out.println("참");
}}
1057. 두 개의 참(1) 또는 거짓(0)이 입력될 때,
참/거짓이 서로 같을 때에만 참이 계산되는 프로그램을 작성해보자.
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
if(x==1 && y==1 || x==0 &&y==0 )
System.out.println("참");
else
System.out.println("거짓");
}}
1058 두 개의 참(1) 또는 거짓(0)이 입력될 때,
모두 거짓일 때에만 참이 계산되는 프로그램을 작성해보자.
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
if(x==0 &&y==0)
System.out.println("참");
else
System.out.println("거짓");
}}
1059. 입력 된 정수를 비트단위로 참/거짓을 바꾼 후 정수로 출력해보자.
비트단위(bitwise)연산자 ~ 를 붙이면 된다.(~ : tilde, 틸드라고 읽는다.)
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x=sc.nextInt();
int tilde =~x;
System.out.println(tilde);
}}
비트 반전 연산자(~), 틸드 연산자
- 정수 타입의 피연산자에만 사용
- 피연연사즐 2진수로 표현했을 때 비트값인 0을 1로, 1은 0으로 반전
- 부호가 반대인 새로운 값이 산출됨
출처: https://hoonmaro.tistory.com/14 [훈마로의 보물창고]
1060. 입력된 정수 두 개를 비트단위로 and 연산한 후 그 결과를 정수로 출력해보자.
비트단위(bitwise)연산자 &를 사용하면 된다.(and, ampersand, 앰퍼센드라고 읽는다.)
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
int and =x&y;
System.out.println(and);
}}
728x90