본문 바로가기
백준

[JAVA] 백준 단계별로 문제풀기 - 5단계 1차원 배열

by Seong-Jun 2022. 2. 21.
728x90
반응형
SMALL

5 - 1. 최대, 최소

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int min = 0, max = 0;
		
		int testCase = sc.nextInt();
		int array[] = new int[testCase];
		
		for(int i = 0; i < testCase; i++) {
			array[i] = sc.nextInt();
		}
		
		min = array[0];
		max = array[0];
		
		for(int i = 0; i < testCase; i++) {
			if(array[i] < min) {
				min = array[i]; // 최솟값
			}
			if(array[i] > max) {
				max = array[i]; // 최댓값
			}
		}
		
		System.out.println(min + " " + max);
	}

}

 

5 - 2. 최댓값

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		StringTokenizer st;

		int max, maxNum = 0;
		int array[] = new int[9];
		int x;
		
		for(int i = 0; i < 9; i++) {
			st = new StringTokenizer(br.readLine());
			x = Integer.parseInt(st.nextToken());
			if(x < 100) {
				array[i] = x;
			}
			else {
				i--;
			}
		}
		
		max = array[0];
		
		for(int i = 0; i < 9; i++) {
			if(array[i] > max) {
				max = array[i];
				maxNum = i;
			}
		}
		bw.write(max + "\n");
		bw.write((maxNum + 1) + "\n");
		
		bw.close();
	}

}

 

5 - 3. 숫자의 개수

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int a, b, c, result;
		int j = 0;
		int array[] = new int[10];
		int arr[] = new int[10];
		
		Scanner sc = new Scanner(System.in);
		
		for(int i = 0; i < 10; i++) {
			array[i] = 0;
		}
		
		a = sc.nextInt();
		b = sc.nextInt();
		c = sc.nextInt();
		
		result = a * b * c;
		while(true) {
			arr[j] = result % 10;
			result = result / 10;
			j++;
			if(result < 10) {
				arr[j] = result;
				break;
			}
		}

		for(int i = 0; i <= j; i++) {
			array[arr[i]]++;
		}
		
		for(int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
	}
}

 

5 - 4. 나머지

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int a, result = 0;
		int arr[] = new int[10];
		
		Scanner sc = new Scanner(System.in);
		
		for(int i = 0; i < 10; i++) {
			a = sc.nextInt();
			arr[i] = a % 42;
		}
		for(int i = 0; i < 10; i++) {
			int count = 0;
			for(int j = i+1; j < 10; j++) {
				if(arr[i] == arr[j]) count++;
			}
			if(count == 0) result++;
		}
		System.out.println(result);
	}
}

 

5 - 5. 평균

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int subjectCount, subjectScore;
		double max = 0;
		double total = 0;
		double average;
		
		Scanner sc = new Scanner(System.in);
		
		subjectCount = sc.nextInt();
		
		double arr[] = new double[subjectCount];
		
		for(int i = 0; i < subjectCount; i++) {
			subjectScore = sc.nextInt();
			arr[i] = subjectScore;
			if(arr[i] > max) {
				max = arr[i];
			}
		}
		for(int i = 0; i < subjectCount; i++) {
			total += arr[i] / max * 100;
		}
		average = total / subjectCount;
		System.out.println(average);
	}
}

 

5 - 6. OX퀴즈

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int testCase, score, sum, n = 0;
		Scanner sc = new Scanner(System.in);
		
		testCase = sc.nextInt();
		
		char[] arr = new char[80];
		char x;
		
		for(int i = 0; i < testCase; i++) {
			sum = 0;
			score = 1;
			
			arr = sc.next().toCharArray();
			
			for(int j = 0; j < arr.length; j++) {
				if(arr[j] == 'O') {
					sum += score;
					score++;
				}
				if(arr[j] == 'X') {
					score = 1;
				}
			}
			System.out.println(sum);
		}
	}
}

 

5 - 7. 평균은 넘겠지

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int testCase, n, score;
		int arr[];
		double average;
		
		Scanner sc = new Scanner(System.in);
		
		testCase = sc.nextInt(); // 테스트 케이스
		
		for(int j = 0; j < testCase; j++) {
			n = sc.nextInt(); // 학생 수
			
			arr = new int[n];
			
			int total = 0;
			
			for(int i = 0; i < n; i++) {
				score = sc.nextInt();
				arr[i] = score;
				total += arr[i]; // 총합
			}
			
			average = total / n; // 평균
			double student = 0;
			
			for(int i = 0; i < n; i++) {
				if(arr[i] > average) {
					student++; // 평균을 넘는 학생 수
				}
			}
			System.out.printf("%.3f%%\n", (student/n)*100);
		}
	}
}

 

728x90
반응형
LIST

댓글