백준 - 2단계 조건문

이상훈·2023년 3월 3일
0

1330번

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(bf.readLine());
        int A = Integer.parseInt(st.nextToken());
        int B = Integer.parseInt(st.nextToken());
        
        if (A > B) {
            System.out.println(">");
        } else if (A < B) {
            System.out.println("<");
        } else {
            System.out.println("==");
        }
    }
}

9498번

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        
        if (A >= 90) {
            System.out.println("A");
        } else if (A >= 80) {
            System.out.println("B");
        } else if (A >= 70) {
            System.out.println("C");
        } else if (A >= 60) {
            System.out.println("D");
        } else {
            System.out.println("F");
        }
    }
}

조건문은 위에서 부터 수행되기 때문에 거름망으로 차례차례 걸러간다 생각하면 좋다.

2753번

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        
        if (A%4==0 && A%100!=0 || A%400==0) {
            System.out.println(1);
        } else {
            System.out.println(0);
        }
    }
}

14681번

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int A = Integer.parseInt(bf.readLine());
        int B = Integer.parseInt(bf.readLine());
        
        if (A>0 && B>0) {
            System.out.println(1);           
        } else if (A<0 && B>0) {
            System.out.println(2);
        } else if (A<0 && B<0) {
            System.out.println(3);
        } else {
            System.out.println(4);
        }
        
    }
}

2884번

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(bf.readLine());
        int A = Integer.parseInt(st.nextToken());
        int B = Integer.parseInt(st.nextToken());
        
        if (B-45 > 0) {
            System.out.println(A+" "+(B-45));
        } else if (B-45 < 0 && A-1<0) {
            int Num = 60 - (45-B);
            System.out.println(23+" "+Num);
        } else if (B-45 < 0 && A-1>=0) {
            int Num = 60 - (45-B);
            System.out.println(A-1+" "+Num);
        } else if (B-45 == 0) {
            System.out.println(A+" "+0);
        }
    }
}

코드가 난잡한데 나중에 리펙토링해보기

2525번

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(bf.readLine());
        int hour = Integer.parseInt(st.nextToken());
        int minute = Integer.parseInt(st.nextToken());
        
        int time = Integer.parseInt(bf.readLine());
        
        int Sum = minute + time;
        if (Sum < 60) {
            System.out.println(hour+" "+Sum);
        } else if (Sum >= 60) {
            for (int i = 0; i<Sum/60; i++) {
                hour++;
                if (hour>=24) {
                    hour=0;
                }
            }
            System.out.println(hour+" "+Sum%60);
        }
    }
}

2884번 보다는 괜찮아졌다...

2480번

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(bf.readLine());
        int first = Integer.parseInt(st.nextToken());
        int second = Integer.parseInt(st.nextToken());
        int third = Integer.parseInt(st.nextToken());
        
        if (first == second && first == third && second == third) {
            System.out.println(10000+first*1000);
        } else if (first == second || first == third) {
            System.out.println(1000+first*100);
        } else if (third == second) {
            System.out.println(1000+second*100);
        } else {
            int MAX = first;
			if (MAX < second) MAX=second;
			if (MAX < third) MAX=third;
			System.out.println(MAX*100);
        }
        
    }
}

&& ||를 통해서 세가지 변수를 한번에 비교 할 수는 없었다. 그래서 나눠서 진행했다.

마무리

0개의 댓글