Study hard

(c++)백준 11652번: 카드 본문

백준/여러가지 문제들

(c++)백준 11652번: 카드

Nimgnoej 2020. 6. 11. 20:54

https://www.acmicpc.net/problem/11652

 

11652번: 카드

준규는 숫자 카드 N장을 가지고 있다. 숫자 카드에는 정수가 하나 적혀있는데, 적혀있는 수는 -262보다 크거나 같고, 262보다 작거나 같다. 준규가 가지고 있는 카드가 주어졌을 때, 가장 많이 가지

www.acmicpc.net

[풀이]

C++ STL의 sort함수를 사용한다.

적혀있는 수의 범위가 크므로 long long타입을 사용한다.

정렬 후 숫자 카드의 개수의 최댓값을 갱신하며 가장 많이 가지고 있는 정수를 찾는다.

 

#include <iostream>
#include <vector>
#include <algorithm>//sort
using namespace std;

int N;
vector<long long>v;

void input() {
	long long n;
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> n;
		v.push_back(n);
	}
}

void solution() {
	input();
	if (N == 1) {
		cout << v[0] << '\n';
		return;
	}
	sort(v.begin(), v.end());
	int cnt = 1;
	long long num = v[0];
	int Max = -1;
	for (int i = 1; i < N; i++) {
		if (v[i - 1] == v[i]) {
			cnt++;
			if (Max < cnt) {
				num = v[i - 1];
				Max = cnt;
			}
		}
		else {
			cnt = 1;
		}
	}
	cout << num << endl;
}

int main() {
	solution();
	return 0;
}