본문 바로가기

프로그래밍/자바

[자바] 정올 579 ~ 586 : 함수2 - 자가진단1~8

728x90
반응형
SMALL

자가진단1

import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        Integer[] arr = new Integer[a];
        for(int i = 0; i < a; i++) arr[i] = sc.nextInt();
        arr_sort(arr);
    }
    public static void arr_sort(Integer[] arr){
        Arrays.sort(arr, Collections.reverseOrder());
        for(int i = 0; i < arr.length; i++) System.out.print(arr[i] + " ");
    }
}

자가진단2

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int mon = sc.nextInt(), day = sc.nextInt();
        if(checkDays(mon, day)) System.out.println("OK!");
        else System.out.println("BAD!");
    }
    public static boolean checkDays(int mon, int day){
        if(mon < 1 || mon > 12 || day < 1 || day  > 31) return false;

        if(mon == 1 ||mon == 3 ||mon == 5 ||mon == 7 ||mon == 8 ||mon == 10 ||mon == 12){
            if(day >= 1 && day <=31) return true;
        }
        if(mon == 4 ||mon == 6 ||mon == 9 ||mon == 11){
            if(day >= 1 && day <=30) return true;
        }
        if(mon == 2){
            if(day >= 1 && day <=28) return true;
        }

        return false;
    }
}

자가진단3

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(), b = sc.nextInt();
        double c = sc.nextDouble(), d = sc.nextDouble();
        System.out.println(find(a,b));
        System.out.printf("%.2f",find(c,d));

    }

    public static double find(double a, double b) {
        return Math.abs(a) < Math.abs(b) ? a : b;
    }

    public static int find(int a, int b) {
        return Math.abs(a) > Math.abs(b) ? a : b;
    }
}

자가진단4

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        System.out.printf("%.2f", find(a));
    }
    public static double find(int a){
        return Math.sqrt(a/3.14);
    }
}

자가진단5

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double[] arr = new double[3];
        for(int i = 0; i < 3; i++)arr[i] = sc.nextDouble();
        solution(arr);
    }
    public static void solution(double[] arr){
        Arrays.sort(arr);
        System.out.print((int)Math.ceil(arr[2]) + " ");
        System.out.print((int)Math.floor(arr[0]) + " ");
        System.out.print((int)Math.round(arr[1]));
    }
}

자가진단6

import java.util.Scanner;
public class Main {
    public static final int ONE = 1;
    public static final int TWO = 2;
    public static final int THREE = 3;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = {ONE, TWO, THREE};
        solution(arr);
    }
    public  static void solution(int[] arr){
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr.length; j++){
                System.out.println(arr[i] + " + " + arr[j] +" = " + (arr[i] + arr[j]));
            }
        }
    }
}

자가진단7

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[10];
        for(int i = 0; i < arr.length; i++) arr[i] = sc.nextInt();
        bubble_sort(arr);
    }
    public static void bubble_sort(int[] arr){

        for(int i = 0; i < arr.length;i++){
            for(int j = 0; j < arr.length-1; j++){
                if(arr[j]<arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
            if(i != arr.length-1){
                for(int k=0;k<arr.length;k++){
                    System.out.print(arr[k] + " ");
                }
                System.out.println();
            }
        }
    }
}

자가진단8

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(), b = sc.nextInt();
        pr_pow(a,b);
    }
    public static void pr_pow(int a, int b){
        System.out.println("("+a+" - "+b+") ^ 2 = "+(int)Math.pow(a-b,2));
        System.out.println("("+a+" + "+b+") ^ 3 = "+(int)Math.pow(a+b,3));
    }
}
728x90
반응형
LIST