알고리즘, 코딩테스트/(JAVA) 코드업 기초100제

(JAVA) 코드업 기초 100제 1047~1060

momo0503 2020. 12. 29. 20:45

1047 : [기초-비트시프트연산] 정수 1개 입력받아 2배 곱해 출력하기

입력 : 1024 (정수 한 개가 입력된다.)

출력 : 2048 (2배 곱한 정수를 출력한다.)

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int input = sc.nextInt();
        
      
        System.out.print(input<<1);
        sc.close();
    }
}

예시
int a=10;
printf("%d", a<<1); //10을 2배 한 값인 20 이 출력된다.
printf("%d", a>>1); //10을 반으로 나눈 값인 5 가 출력된다.
printf("%d", a<<2); //10을 4배 한 값인 40 이 출력된다.
printf("%d", a>>2); //10을 반으로 나눈 후 다시 반으로 나눈 값인 2 가 출력된다.

 

 

1048 : [기초-비트시프트연산] 한 번에 2의 거듭제곱 배로 출력하기

입력 : 1 3

출력 : 8 (a 를 2b배 만큼 곱한 값을 출력한다.)

정수 2개(a, b)를 입력받아 a를 2b배 곱한 값으로 출력해보자.
0 <= a <= 10, 0 <= b <= 10

예시
int a=1, b=10;
printf("%d", a << b); //210 = 1024 가 출력된다.

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int input = sc.nextInt();
        int input2 = sc.nextInt();
        
      
        System.out.print(input<<input2);
        sc.close();
    }
}

 

 

1049 : [기초-비교연산] 두 정수 입력받아 비교하기1

입력 : 9 1

출력 : 1(a가 b보다 큰 경우 1을, 그렇지 않은 경우 0을 출력한다.)

//1(참)

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int input = sc.nextInt();
        int input2 = sc.nextInt();
        sc.close();
      
        if(input>input2) System.out.print(1);
        else System.out.print(0);
        
    }
}

 

 

1050 : [기초-비교연산] 두 정수 입력받아 비교하기2

a와 b의 값이 같은 경우 1을, 그렇지 않은 경우 0을 출력한다.

입력 : 0 0

출력 : 1

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int input = sc.nextInt();
        int input2 = sc.nextInt();
        
        if(input == input2) System.out.print(1);
        else System.out.print(0);
      
       
        sc.close();
    }
}

 

 

1051 : [기초-비교연산] 두 정수 입력받아 비교하기3

b가 a보다 크거나 같은 경우 1을, 그렇지 않은 경우 0을 출력한다.

입력 : 0 -1

출력 : 0

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int input = sc.nextInt();
        int input2 = sc.nextInt();
        
        if(input <= input2) System.out.print(1);
        else System.out.print(0);
      
       
        sc.close();
    }
}

 

 

1052 : [기초-비교연산] 두 정수 입력받아 비교하기4

입력 : 0 1

출력 : 0

a와 b가 다른 경우 1을, 그렇지 않은 경우 0을 출력한다.

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int input = sc.nextInt();
        int input2 = sc.nextInt();
        
        if(input != input2) System.out.print(1);
        else System.out.print(0);
      
       
        sc.close();
    }
}

 

 

1053 : [기초-논리연산] 참 거짓 바꾸기

입력 : 1(정수 1개가 입력된다.(단, 0 또는 1 만 입력된다.))

출력 : 0(입력된 값이 0이면 1, 1이면 0을 출력한다.)

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int input = sc.nextInt();
        
        if(input == 1) System.out.print(0);
        else System.out.print(1);
      
       
        sc.close();
    }
}

 

 

1054 : [기초-논리연산] 둘 다 참일 경우만 참 출력하기

입력 : 1 1(1 또는 0의 값만 가지는 2개의 정수가 공백을 두고 입력된다.)

출력 : 1(둘 다 참(1)일 경우에만 1을 출력하고, 그 외의 경우에는 0을 출력한다.)

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int input = sc.nextInt();
        int input2 = sc.nextInt();
        
        if(input==1&&input2==1) System.out.print(1);
        else System.out.print(0);
      
       
        sc.close();
    }
}

 

 

1055 : [기초-논리연산] 하나라도 참이면 참 출력하기

입력 : 1 1 (1 또는 0의 값만 가지는 2개의 정수가 공백을 두고 입력된다.)

출력 : 1 (하나라도 참일 경우 1을 출력하고, 그 외의 경우에는 0을 출력한다.)

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int input = sc.nextInt();
        int input2 = sc.nextInt();
        
        if(input==1 || input2==1) System.out.print(1);
        else System.out.print(0);
      
       
        sc.close();
    }
}

 

 

1056 : [기초-논리연산] 참/거짓이 서로 다를 때에만 참 출력하기

입력 : 1 1 (1 또는 0의 값만 가지는 2개의 정수가 공백을 두고 입력된다.)

출력 : 0 (참/거짓이 서로 다를 때에만 1을 출력하고, 그 외의 경우에는 0을 출력한다.)

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int input = sc.nextInt();
        int input2 = sc.nextInt();
        
        if(input == input2) System.out.print(0);
        else System.out.print(1);
      
       
        sc.close();
    }
}

 

 

1057 : [기초-논리연산] 참/거짓이 서로 같을 때에만 참 출력하기

입력 : 0 0(1 또는 0의 값만 가지는 2개의 정수가 공백을 두고 입력된다.)

출력 : 1(참/거짓이 서로 같을 때에만 1을 출력하고, 그 외의 경우에는 0을 출력한다.)

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int input = sc.nextInt();
        int input2 = sc.nextInt();
        
        if(input == input2) System.out.print(1);
        else System.out.print(0);
      
       
        sc.close();
    }
}

 

 

1058 : [기초-논리연산] 둘 다 거짓일 경우만 참 출력하기

입력 : 0 1 

출력 : 0(둘 다 거짓일 경우에만 1을 출력하고, 그 외의 경우에는 0을 출력한다.)

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int input = sc.nextInt();
        int input2 = sc.nextInt();
        
        if(input == 0 && input2 == 0) System.out.print(1);
        else System.out.print(0);
      
       
        sc.close();
    }
}

 

 

1059 : [기초-비트단위논리연산] 비트단위로 NOT 하여 출력하기

입력 : 2

출력 : -3 (비트 단위로 1 -> 0, 0 -> 1로 바꾼 후 그 값을 10진수로 출력한다.)

** 비트단위(bitwise) 연산자는,
~(bitwise not), &(bitwise and), |(bitwise or), ^(bitwise xor),
<<(bitwise left shift), >>(bitwise right shift)
가 있다.

예를 들어 1이 입력되었을 때 저장되는 1을 32비트 2진수로 표현하면
        00000000 00000000 00000000 00000001 이고,
~1은 11111111 11111111 11111111 11111110 가 되는데 이는 -2를 의미한다.

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int input = sc.nextInt();
        
        System.out.print(~input);
      
       
        sc.close();
    }
}

 

 

1060 : [기초-비트단위논리연산] 비트단위로 AND 하여 출력하기

입력 : 3 5

출력 : 1

입력된 정수 두 개를 비트단위로 and 연산한 후 그 결과를 정수로 출력해보자.
비트단위(bitwise)연산자 &를 사용하면 된다.(and, ampersand, 앰퍼센드라고 읽는다.)


** 비트단위(bitwise)연산자는,
~(bitwise not), &(bitwise and), |(bitwise or), ^(bitwise xor),
<<(bitwise left shift), >>(bitwise right shift)
가 있다.

예를 들어 3과 5가 입력되었을 때를 살펴보면
3       : 00000000 00000000 00000000 00000011
5       : 00000000 00000000 00000000 00000101
3 & 5 : 00000000 00000000 00000000 00000001
이 된다.

비트단위 and 연산은 두 비트열이 주어졌을 때,
둘 다 1인 부분의 자리만 1로 만들어주는 것과 같다.

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int input = sc.nextInt();
        int input2 = sc.nextInt();
        
       System.out.print(input&input2);
      
       
        sc.close();
    }
}