본문 바로가기

숨막히는 알고말고/문제 풀이

[SWEA] 아름이의 돌 던지기

👀 문제 설명

문제

로그인해야 문제를 볼 수 있다

 

✍🏻풀이

완전 탐색으로, 각 사람이 돌을 던졌을 때 위치의 절댓값 중 최솟값을 찾으면 된다.

최솟값을 저장해두고, 다시 완전탐색으로 해당하는 사람이 몇명인지 구한다.

 

코드

C++

//  아름이의 돌 던지기 (https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV18-stqI8oCFAZN&categoryId=AV18-stqI8oCFAZN&categoryType=CODE&problemTitle=1285&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1)

#include <iostream>
#include <vector>
#include <math.h>

#define initIO ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);

using namespace std;

int main() {
    initIO;

    int T, N;
    vector<int> peoples;
    cin >> T;

    for (int test_case = 1; test_case <= T; test_case++) {
        cin >> N;

        peoples.resize(N);
        int min = 100001;
        for (int i = 0; i < N; i++) {
            cin >> peoples.at(i);

            if (min > abs(peoples.at(i))) {
                min = abs(peoples.at(i));
            }
        }

        int count = 0;
        for (int i = 0; i < N; i++) {
            if (abs(peoples.at(i)) == min)
                count++;
        }

        cout << "#" << test_case << " " << min << " " << count << "\n";
    }

    return 0;
}

 

JAVA

Java는 제출할 수 없게 되어있는데, 그냥 C++을 Java로 풀어봤다..

package swea;

import java.io.*;
import java.util.*;

// 아름이의 돌 던지기 (https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV18-stqI8oCFAZN&categoryId=AV18-stqI8oCFAZN&categoryType=CODE&problemTitle=1285&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1)

public class SWEA_1285 {

	private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	private static StringTokenizer st;
	private static int T;
	private static int N;
	
	public static void main(String[] args) throws Exception {
		T = Integer.parseInt(br.readLine());
		
		for (int test_case = 1; test_case <= T; test_case++) {
			// input
			N = Integer.parseInt(br.readLine());
			int[] peoples = new int[N];
			
			st = new StringTokenizer(br.readLine());
			
			int min = 100001;
			for (int i = 0; i < N; i++) {
				peoples[i] = Integer.parseInt(st.nextToken());
				
				if (min > Math.abs(peoples[i])) {
					min = Math.abs(peoples[i]);
				}
			}
			
			int count = 0;
			for (int i = 0; i < N; i++) {
				if (Math.abs(peoples[i]) == min) {
					count++;
				}
			}
			
			System.out.println("#" + test_case + " " + min + " " + count);
		}
		
	}

}

'숨막히는 알고말고 > 문제 풀이' 카테고리의 다른 글

[Baekjoon] 퇴사  (2) 2021.08.01
[SWEA] 구구단2  (0) 2021.07.29
[Baekjoon] 오목  (0) 2021.07.27
[Baekjoon] 수 찾기  (0) 2021.07.20
[Baekjoon] 입국심사  (0) 2021.07.19