1차원 배열 [Java]

sua·2022년 9월 17일
0
post-thumbnail

10818번 최소, 최대

문제


풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;

        for(int i = 0; i < n; i++) {
            int a = sc.nextInt();
            if(a < min) { 
                min = a;
            }
            if(a > max) {
                max = a;
            }
        }

        System.out.println(min + " " + max);
        
        sc.close();
    }
}



2562번 최댓값

문제


풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int max = 0;
        int index = -1;

        for(int i = 0; i < 9; i++) {
            int n = sc.nextInt();
            if(max < n) {
                max = n;
                index = i + 1;
            }
        }

        System.out.println(max);
        System.out.println(index);
        
        sc.close();
    }
}



3052번 나머지

문제


풀이

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        List<Integer> list = new ArrayList<>();
        int cnt = 0;
        
        for(int i = 0; i < 10; i++) {
            int n = sc.nextInt();
            if(!list.contains(n % 42)) {
                list.add(n % 42);
                cnt++;
            }
        }

        System.out.println(cnt);
        
        sc.close();
    }
}



1546번 평균

문제


풀이

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		int[] arr = new int[n];
		int max = -1;
		double sum = 0.0;
		
		for (int i = 0; i < arr.length; i++) {
			arr[i] = sc.nextInt();
			if (arr[i] > max) {
				max = arr[i];
			}
			sum += arr[i];
		}
		System.out.println(((sum / max) * 100) / n);
        
        sc.close();
	}
}



8958번 OX퀴즈

문제


풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args)  {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        
        for(int i = 0; i < n; i++) {
            String str = sc.next();
            int sum = 0;
            int cnt = 0;
            for(int j = 0; j < str.length(); j++) {
                if(str.charAt(j) == 'O') {
                    cnt++;
                    sum += cnt;
                } else {
                    cnt = 0;
                }
            }
            System.out.println(sum);
        }
        
        sc.close();
    }
}



4344번 평균은 넘겠지

문제


풀이

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();	

        for(int i = 0; i < n; i++) {
            int a = sc.nextInt();	
            int[] score = new int[a];
            double sum = 0.0;	
            for(int j = 0; j < a; j++) { 
                score[j] = sc.nextInt();
                sum += score[j];
            }

            double avg = sum / a; 
            int cnt = 0; 
            for(int j = 0; j < a; j++) {
                if(score[j] > avg) { 
                    cnt++;
                }
            }

            String percent = String.format("%.3f", ((double)cnt / a) * 100);
            System.out.println(percent + "%");
        }
        
        sc.close();
    }
}
profile
가보자고

0개의 댓글