본문 바로가기

코딩테스트 연습/SWEA

[SWEA] 1285. 아름이의 돌 던지기 D2 (c++)

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&contestProbId=AV18-stqI8oCFAZN&categoryId=AV18-stqI8oCFAZN&categoryType=CODE&problemTitle=&orderBy=INQUERY_COUNT&selectCodeLang=ALL&select-1=2&pageSize=10&pageIndex=3 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

입력받은 값의 절댓값을 person에 저장해서, 오름차순으로 정렬하여 Min = person[0]으로 저장하고,

Min과 절댓값이 같은 수만큼 cnt++ 를 해줬다.

 

#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 1001

bool comp (int a,int b){
    return a < b;
}
int main() {

    int T;
    cin >> T;
    for(int t=1; t<=T; t++) {
        int n;
        cin >> n;
        int person[MAX] = {0,};
        for(int i=0;i<n;i++) cin >> person[i];
        
        int dist = 0, cnt = 0;
        
        for(int i=0;i<n;i++) if(person[i] < 0) person[i] = -person[i];
    
        sort(person, person+n, comp); //오름차순 정렬
        int Min = person[0];
        for(int i=0;i<n;i++){
            if(Min == person[i])cnt++;
        }
        
        cout << "#" << t << " " << Min << " " << cnt << "\n";
    }
    
    return 0;
}