정렬 관련 문제...!. 벌써 2주차지만 이 문제는 1주차의 마지막 문제였다.
정말 간단했는데, 10진수로 들어온 값을 이진수로 변환해서 1비트의 개수를 세어주면 되는 문제이다. 나는 pair를 이용해서 풀었고, 굳이 이진수로 값을 변환하지 않고 1비트가 나오는 경우만 세줬다.
비트수가 같다면 십진수로 더 큰 값을 앞에 세우라는 조건때문에 오랜만에 커스텀 함수를 이용해서 정렬했다.
간단한 문제였지만...생각보다 푸는데 오래걸렸다. 2진수 변환은 밥먹듯이 해도 모자란데 아직도 어렵다 🥲
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(pair<int,int> a, pair<int,int> b){
if (a.second<b.second)
return false;
else if (a.second>b.second)
return true;
else
{
if (a.first<=b.first)
return false;
else
return true;
}
}
int main() {
int n,k,a,temp;
vector<pair<int,int>>bi;
cin>>n>>k;
for(int i=0;i<n;i++){
cin>>a;
temp=0;
int tmp=a;
while (tmp>=1)
{
if(tmp%2==1)
temp++;
tmp/=2;
}
bi.push_back({a,temp});
}
sort(bi.begin(),bi.end(),compare);
cout<<bi[k - 1].first;
return 0;
}