Hayden's Archive

[알고리즘] 백준 1546번 : 평균 / 백준 2577번 : 숫자의 개수 / 백준 3052번 : 나머지 본문

Algorithm

[알고리즘] 백준 1546번 : 평균 / 백준 2577번 : 숫자의 개수 / 백준 3052번 : 나머지

_hayden 2020. 6. 2. 10:16

알고리즘 문제 출처 : https://www.acmicpc.net/problem/1546

따로 최댓값으로 나눈 뒤 100을 곱하고 합하는 것이나 총합을 최댓값으로 나눈 뒤 100을 곱하는 것이나 결과는 같게 나온다. 그런 생각에서 코드를 짰는데 결과가 이상하게 나와서 당황스러웠다. 알고 보니 모두 int형으로만 변수를 선언해서 나눈 결과가 온전히 나오지 안고 몫만 나왔던 것. 필요한 숫자들을 따로 double형으로 선언하였고 입력값도 double형으로 받으니 정답!

import java.util.Scanner;

public class Main {

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

 


알고리즘 문제 출처 : https://www.acmicpc.net/problem/2577

입력되는 세 수를 먼저 곱한 뒤 나머지의 개수를 받을 사이즈 10의 배열을 만든다. 그리고 10으로 나눈 나머지와 배열의 인덱스를 비교하여 해당되는 인덱스에 +1을 해줘서 count를 매긴다.

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int multiply = a * b * c;
		int[] count = new int[10];
		while(true) {
			count[multiply%10] += 1;
			multiply=multiply/10;
			if(multiply<10) {
				count[multiply] += 1;
				break;
			}
		}
		for(int i=0; i<10; i++) {
			System.out.println(count[i]);
		}
	}
}

 


알고리즘 문제 출처 : https://www.acmicpc.net/problem/3052

내가 작성한 코드

import java.util.Scanner;

public class Main {

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