코드업 기초 100제 (자바): 1047-1077

8 minute read

모든 문제는 코드업에서 참고했습니다.

기초-비트시프트연산

1047 : 정수 1개 입력받아 2배 곱해 출력

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

1048 : 한 번에 2의 거듭제곱 배로 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        // given (a,b) where a,b integers, find a * (2^b)
        Scanner scanner = new Scanner(System.in);
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);
        System.out.println( a << b);
        scanner.close();
    }
}

기초-비교연산

1049 : 두 정수 입력받아 비교1

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);

        if (a > b) {
            System.out.println(1);
        } else {
            System.out.println(0);
        }
        scanner.close();
    }
}

or

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);

        System.out.println((a>b) ? 1 : 0);
        scanner.close();
    }
}

1050 : 두 정수 입력받아 비교2

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);

        if (a == b) {
            System.out.println(1);
        } else {
            System.out.println(0);
        }
        scanner.close();
    }
}

or

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);

        System.out.println((a==b) ? 1 : 0);
        scanner.close();
    }
}

1051 : 두 정수 입력받아 비교3

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);

        if (a <= b) {
            System.out.println(1);
        } else {
            System.out.println(0);
        }

        //or System.out.println((a<=b) ? 1 : 0);
        scanner.close();
    }
}

1052 : 두 정수 입력받아 비교4

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);

        if (a != b) {
            System.out.println(1);
        } else {
            System.out.println(0);
        }

        //System.out.println((a!=b) ? 1 : 0);
        scanner.close();
    }
}

기초-논리연산

1053 : 참 거짓 바꾸기

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int input = scanner.nextInt();
        boolean isZero = (input == 0);
        System.out.println(isZero ? 1: 0);
        scanner.close();
    }
}

or to use ! operator,

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int input = scanner.nextInt();
        boolean isNonZero = (input != 0);
        System.out.println(!isNonZero ? 1: 0);
        scanner.close();
    }
}

1054 : 둘 다 참일 경우만 참 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);

        boolean bothTrue = (a == 1) && (b == 1);
        System.out.println(bothTrue ? 1 : 0);
        scanner.close();
    }
}

1055 : 하나라도 참이면 참 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);

        boolean eitherTrue = (a == 1) || (b == 1);
        System.out.println(eitherTrue ? 1 : 0);
        scanner.close();
    }
}

1056 : 참/거짓이 서로 다를 때에만 참 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);

        boolean bothDiff = (a == 1) ^ (b == 1);
        System.out.println(bothDiff ? 1 : 0);
        scanner.close();
    }
}

or

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);

        boolean aTrue = a != 0;
        boolean bTrue = b != 0;

     // boolean bothDiff = (a == 1 && b != 1) || (a != 1 && b == 1);
        boolean bothDiff = (aTrue && !bTrue) || (!aTrue && bTrue);
        System.out.println(bothDiff ? 1 : 0);
        scanner.close();
    }
}

1057 : 참/거짓이 서로 같을 때에만 참 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);

        boolean bothSame = !((a == 1) ^ (b == 1));
        System.out.println(bothSame ? 1 : 0);
        // boolean bothDiff = ((a == 1) ^ (b == 1));
        // System.out.println(bothDiff ? 0 : 1);
        scanner.close();
    }
}

or

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);

        boolean aTrue = a != 0;
        boolean bTrue = b != 0;

     // boolean bothDiff = (a == 1 && b == 1) || (a != 1 && b != 1);
        boolean bothSame = (aTrue && bTrue) || (!aTrue && !bTrue);
        System.out.println(bothSame ? 1 : 0);
        scanner.close();
    }
}

1058 : 둘 다 거짓일 경우만 참 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        // !A && !B <-> !(A || B)
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);

        boolean bothFalse = !((a == 1) || (b == 1));
        // boolean bothFalse = !(a == 1) && !(b == 1);
        System.out.println(bothFalse ? 1 : 0);
        scanner.close();
    }
}

기초-비트단위논리연산

1059 : 비트단위로 NOT 하여 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        int input = scanner.nextInt();
        int complement = ~input;
        System.out.println(complement);
        scanner.close();
    }
}

1060 : 비트단위로 AND 하여 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int inputFirst = Integer.parseInt(inputArray[0]);
        int inputSecond = Integer.parseInt(inputArray[1]);

        int firstAndSecond = inputFirst & inputSecond;
        System.out.println(firstAndSecond);
        scanner.close();
    }
}

1061 : 비트단위로 OR 하여 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int inputFirst = Integer.parseInt(inputArray[0]);
        int inputSecond = Integer.parseInt(inputArray[1]);

        int firstOrSecond = inputFirst | inputSecond;
        System.out.println(firstOrSecond);
        scanner.close();
    }
}

1062 : 비트단위로 XOR 하여 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int inputFirst = Integer.parseInt(inputArray[0]);
        int inputSecond = Integer.parseInt(inputArray[1]);

        int firstXorSecond = inputFirst ^ inputSecond;
        System.out.println(firstXorSecond);
        scanner.close();
    }
}

기초-삼항연산

1063 : 두 정수 입력받아 큰 수 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int inputFirst = Integer.parseInt(inputArray[0]);
        int inputSecond = Integer.parseInt(inputArray[1]);

        System.out.println((inputFirst > inputSecond) ? inputFirst : inputSecond);
        scanner.close();
    }
}

1064 : 정수 3개 입력받아 가장 작은 수 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");
        int a = Integer.parseInt(inputArray[0]);
        int b = Integer.parseInt(inputArray[1]);
        int c = Integer.parseInt(inputArray[2]);

        int smallestInput = (a < b ? a : b) < c ? (a > b ? b : a) : c;
/* OR
        int smallestInput = a < b ?
                (a < c ? a : c):
                (b < c ? b : c);
*/
        System.out.println(smallestInput);
        System.out.println(smallestInput);
        scanner.close();
    }
}

기초-조건/선택실행구조

1065 : 정수 3개 입력받아 짝수만 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");

        for (String s: inputArray) {
            int num = Integer.parseInt(s);
            if (num%2 == 0) {
                System.out.println(num);
            }
        }
        scanner.close();
    }
}

1066 : 정수 3개 입력받아 짝/홀 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");

        for (String s: inputArray) {
            int num = Integer.parseInt(s);
            if (num%2 == 0) {
                System.out.println("even");
            } else {
                System.out.println("odd");
            }
        }
        scanner.close();
    }
}

1067 : 정수 1개 입력받아 분석

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        int input = scanner.nextInt();

        if (input >= 0) {
            System.out.println("plus");
        } else {
            System.out.println("minus");
        }

        if (input%2 == 0) {
            System.out.println("even");
        } else {
            System.out.println("odd");
        }
        scanner.close();
    }
}

1068 : 정수 1개 입력받아 평가 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        int input = scanner.nextInt();

        // 0<= input <= 100
        if (input >= 90) {
            System.out.println("A");
        } else if (input >= 70) {
            System.out.println("B");
        } else if (input >= 40) {
            System.out.println("C");
        } else {
            System.out.println("D");
        }
        scanner.close();
    }
}

1069 : 평가 입력받아 다르게 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        char input = scanner.nextLine().charAt(0);
        switch (input) {
            case 'A':
                System.out.println("best!!!");
                break;
            case 'B':
                System.out.println("good!!");
                break;
            case 'C':
                System.out.println("run!");
                break;
            case 'D':
                System.out.println("slowly~");
                break;
            default:
                System.out.println("what?");
        }
        scanner.close();
    }
}

1070 : 월 입력받아 계절 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        //Java 14 version
        int input = scanner.nextInt();

        String season = switch (input) {
            case 12,1,2 -> "winter";
            case 3,4,5 -> "spring";
            case 6,7,8 -> "summer";
            case 9,10,11 -> "fall";
            default -> "";
        };
        System.out.println(season);
        scanner.close();
    }
}

or

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        int input = scanner.nextInt();

        switch(input) {
            case 12:
            case 1:
            case 2:
                System.out.println("winter");
                break;
            case 3:
            case 4:
            case 5:
                System.out.println("spring");
                break;
            case 6:
            case 7:
            case 8:
                System.out.println("summer");
                break;
            case 9:
            case 10:
            case 11:
                System.out.println("fall");
                break;
        }
        scanner.close();
    }
}

기초-반복실행구조

1071 : 0 입력될 때까지 무한 출력1

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String[] inputArray = scanner.nextLine().split(" ");

        for (String s: inputArray) {

            if (s.equals("0")) {
                break;
            }
            System.out.println(s);
        }
        scanner.close();
    }
}

1072 : 정수 입력받아 계속 출력하기

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        String count = scanner.nextLine();
        String[] inputArray = scanner.nextLine().split(" ");

        for (String s: inputArray) {
            System.out.println(s);
        }
        scanner.close();
    }
}

1073 : 0 입력될 때까지 무한 출력2

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] inputArray = br.readLine().split(" ");

        for (String s: inputArray) {
            if (s.equals("0")) {
                break;
            }
            System.out.println(s);
        }
        scanner.close();
    }
}

1074 : 정수 1개 입력받아 카운트다운 출력1

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        int count = scanner.nextInt();

        while (count > 0) {
            System.out.println(count--);
        }
        scanner.close();
    }
}

1075 : 정수 1개 입력받아 카운트다운 출력2

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        int count = scanner.nextInt();

        while (count > 0) {
            System.out.println(--count);
        }
        scanner.close();
    }
}

1076 : 문자 1개 입력받아 알파벳 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        int a = 'a'; //97
        char inputChar = scanner.next().charAt(0);

        for (int i = a; i <= (int)inputChar; i++) {
            System.out.println((char)i);
        }
        scanner.close();
    }
}

1077 : 정수 1개 입력받아 그 수까지 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        int input = scanner.nextInt();

        for (int i = 0; i <= input; i++) {
            System.out.println(i);
        }
        scanner.close();
    }
}

Leave a comment