본문 바로가기

프로그래밍/자바

[자바] 정올 555 ~ 563 : 배열1 - 자가진단 1 ~ 9

728x90
반응형
SMALL

자가진단1- 555

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

자가진단2 - 556

public class Main {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        for(int i = 0; i < arr.length; i++) System.out.print(arr[i] + " ");
    }
}

자가진단3 - 557

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] arr = new char[11];
        for(int i = 1; i < 11; i++){
            arr[i] = sc.next().charAt(0);
        }
        System.out.println(arr[1] +" "+arr[4]+" "+arr[7]);
    }
}

자가진단4 - 558

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[100];
        int n,a;
        for(n = 0; n < 100; n++){
            a = sc.nextInt();
            if(a==0)break;
            arr[n] = a;
        }
        for(int i = n-1; i >= 0; i--){
            System.out.print(arr[i] + " ");
        }
    }
}

자가진단5 - 559

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double[] arr = {0.0, 85.6, 79.5, 83.1, 80.0, 78.2, 75.0};
        int a = sc.nextInt(), b = sc.nextInt();
        double sum = arr[a]+arr[b];
        System.out.printf("%.1f", sum);
    }
}

자가진단6 - 560

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int min = 1001,n;
        for(int i = 0; i < 10; i ++){
            n = sc.nextInt();
            min = min < n ? min : n;
        }
        System.out.println(min);
    }
}

자가진단7- 561

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 < 10; i++) arr[i] = sc.nextInt();
        int max_under = -999999;
        int min_over = 999999;
        for(int i = 0; i < arr.length; i++){
            if(arr[i] < 100){
                max_under = max_under > arr[i] ? max_under :arr[i];
            }else{
                min_over = min_over < arr[i] ? min_over :arr[i];
            }
        }
        if(max_under < 0) max_under = 100;
        if(min_over == 999999) min_over = 100;
        System.out.print(max_under + " " + min_over);
    }
}

자가진단8 - 562

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 < 10; i++) arr[i] = sc.nextInt();
        int even = 0, odd = 0;
        for(int i = 0; i < 10; i++){
            if((i+1)%2 == 0) even += arr[i];
            else odd += arr[i];
        }
        System.out.println("sum : " + even);
        System.out.printf("avg : %.1f", odd/5.0);
    }
}

자가진단9 - 563

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

        Arrays.sort(arr, Collections.reverseOrder());
        for(int i = 0; i < arr.length; i++) System.out.print(arr[i] + " ");
    }
}
728x90
반응형
LIST