[CodeUp] C언어 기초 100제 (1061~1070) - Java

황은하·2021년 5월 7일
0

알고리즘

목록 보기
28/100
post-thumbnail

✔ 1061

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        System.out.println(a | b);
    }
}

✔ 1062

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        System.out.println(a ^ b);
    }
}

둘이 같으면 0, 다르면 1 출력한다. XOR


✔ 1063

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int result = (a > b) ? a : b;
        System.out.println(result);
    }
}

삼항연산자를 이용해 큰 수를 출력한다. 삼항연산자를 사용할 때에는 앞에 변수를 지정해줘야 한다.


✔ 1064

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int result = (((a < b) ? a : b) < c) ? (a < b) ? a : b : c;
        System.out.println(result);
    }
}

✔ 1065

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        if(a % 2 == 0) System.out.println(a);
        if(b % 2 == 0) System.out.println(b);
        if(c % 2 == 0) System.out.println(c);
    }
}

✔ 1066

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] arr = sc.nextLine().split(" ");
        for (String s : arr) {
            if (Integer.parseInt(s) % 2 == 0) {
                System.out.println("even");
            } else {
                System.out.println("odd");
            }
        }
    }
}

✔ 1067

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if(n > 0){
            System.out.println("plus");
        }else{
            System.out.println("minus");
        }
        if(n % 2 == 0){
            System.out.println("even");
        }else{
            System.out.println("odd");
        }
    }
}

✔ 1068

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n > 89) {
            System.out.println("A");
        } else if (n > 69) {
            System.out.println("B");
        } else if (n > 39) {
            System.out.println("C");
        } else {
            System.out.println("D");
        }
    }
}

✔ 1069

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        String grade = sc.nextLine();
        switch(grade){
            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?");
        }
    }
}

JDK7 이전에는 switch다음 괄호안에 정수타입의 변수만 올 수 있었습니다. 그런데 JDK7 부터는 switch다음 괄호안에 문자열 타입의 변수도 올 수 있습니다 - 프로그래머스


✔ 1070

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int season = sc.nextInt();
        switch(season){
            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;
        }
    }
}

switch문에서 break를 걸지 않으면 다음 명령이 실행된다. 이렇게 작성하면 세 케이스가 묶여서 하나의 공통 단어를 출력하게 된다.

profile
차근차근 하나씩

0개의 댓글