题目链接:点击打开链接
题目大意:开始有一个数x,你可以操作n-1次:1,除3(x必须被3整除才行);2,乘2。每操作一次,就把结果写在那个数的后面,所以最终你会得到n个数,现在你得到了一个乱序后的结果,让你输出可能的有序的游戏结果。
解题思路:由于能整除3才可能除3,所以看这些数的因子中3的个数,多的放在前面,少的放在后面,如果一样的话(比如12, 6),就把小的的放在前面(6, 12才能行得通)。
用到了一个比较巧妙的sort
还有就是数据范围较大应该开ll
代码:
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<string> #include<iomanip> #include<vector> #include<map> #include<set> #define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) typedef long long ll; const int inf = 0x3f3f3f3f; const int mod = (int)1e9 + 9; const int maxn = (int)1e5 + 5; using namespace std; int three(ll n){ int res = 0; while(!(n % 3)) res++, n /= 3; return res; } bool cmp(ll x, ll y){ int thrx = three(x), thry = three(y); if(thrx == thry) return x < y; return thrx > thry; } ll a[105]; int main() { int n; cin >> n; for(int i = 0; i < n; i++){ cin >> a[i]; } sort(a, a + n, cmp); for(int i = 0; i < n; i++){ cout << a[i] << ' '; } cout << endl; return 0; }