알고리즘
백준 16936번 나3곱2 C++
영춘권의달인
2023. 2. 3. 13:07
https://www.acmicpc.net/problem/16936
16936번: 나3곱2
나3곱2 게임은 정수 하나를 이용한다. 가장 먼저, 정수 x로 시작하고, 연산을 N-1번 적용한다. 적용할 수 있는 연산은 두 가지 있고, 아래와 같다. 나3: x를 3으로 나눈다. x는 3으로 나누어 떨어져야
www.acmicpc.net
3으로 많이 나누어 떨어지고 2로 적게 나누어 떨어지는 순서대로 정렬했다.
이렇게 정렬하면 앞에서부터 3으로 나누다가 2로 곱하는 순서로 수열을 만들 수 있다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
struct Val {
Val() {}
Val(int64 val) {
cnt_3 = 0;
cnt_2 = 0;
this->val = val;
while (true) {
if (val % 3 != 0) break;
val /= 3;
cnt_3++;
}
while (true) {
if (val % 2 != 0) break;
val /= 2;
cnt_2++;
}
}
int64 val;
int cnt_3;
int cnt_2;
bool operator<(const Val& other) {
if (cnt_3 == other.cnt_3) {
return cnt_2 < other.cnt_2;
}
return cnt_3 > other.cnt_3;
}
};
int n;
vector<Val> v;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
v = vector<Val>(n);
for (int i = 0; i < n; i++) {
int64 b;
cin >> b;
v[i] = { b };
}
sort(v.begin(), v.end());
for (int i = 0; i < n; i++) {
cout << v[i].val << " ";
}
}