본문 바로가기

프로그래밍/자바

[자바] 정올 564 ~ 570 : 배열2 - 자가진단1 ~ 7

728x90
반응형
SMALL

자가진단1

 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] arr = new char[100];
        int[] arr2 = new int[26];
        char ch;
        int i;
        for(i = 0; i < 100;i++){
            ch = sc.next().charAt(0);
            if((int)ch == 32) continue;
            if((int)ch < 65 || (int)ch >90) break;
            arr[i] = ch;
            arr2[(int)ch - 65]++;
        }
        for(int j = 0; j < arr2.length; j++){
            if(arr2[j] == 0) continue;
            System.out.println(((char)(j + 65)) + " : " + arr2[j]);
        }

    }
}

자가진단2

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[10];
        int a;
        while(true){
            a = sc.nextInt();
            if(a == 0) break;
            arr[a/10]++;
        }
        for(int i = 0; i < arr.length; i++){
            if(arr[i] == 0) continue;
            System.out.println(i + " : " + arr[i]);
        }
    }
}

자가진단3

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

자가진단4

public class Main {
    public static void main(String[] args) {
        int[][] arr = { {5,    8,   10,    6,    4},
                {11,   20,    1,   13,    2},
                {7,    9,   14,   22,    3}};
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr[i].length; j++){
                System.out.printf("%2d   ",arr[i][j]);
            }
            System.out.println();
        }
    }
}

자가진단5

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[][] fa = new int[2][4];
        int[][] sa = new int[2][4];
        System.out.println("first array");

        for(int i = 0; i < fa.length; i++){
            for(int j = 0; j < fa[i].length; j++){
                fa[i][j] = sc.nextInt();
            }
            sc.nextLine();
        }
        System.out.println("second array");
        for(int i = 0; i < sa.length; i++){
            for(int j = 0; j < sa[i].length; j++){
                sa[i][j] = sc.nextInt();
            }
        }

        for(int i = 0; i < sa.length; i++){
            for(int j = 0; j < sa[i].length; j++){
                System.out.print((fa[i][j] * sa[i][j]) + " ");
            }
            System.out.println();
        }
    }

}

자가진단6

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

        }//end for
        int sum = 0, count = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                sum += arr[i][j];
            }

            if(sum / 4 >= 80){
                System.out.println("pass");
                count++;
            }
            else System.out.println("fail");

            sum = 0;
        }
        System.out.println("Successful : " + count);

    }//end main
}

자가진단7

public class Main {
    public static void main(String[] args) {
        int[][] arr = new int[5][5];
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr[i].length; j++){
                if(i == 0 || j == 0) arr[i][j] = 1;
                else arr[i][j] = arr[i-1][j] + arr[i][j-1];
            }
        }
        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr[i].length; j++){
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }

    }
}
728x90
반응형
LIST