AC代码:
#include<bits/stdc++.h>
using namespace std;
long long a[107];
map<long long,int> mp;
stack<long long> s;
int check1(long long x){
if(mp[x*3]) return 1;
else return 0;
}
int check2(long long x){
if (x%2==0 && mp[x/2]) return 1;
else return 0;
}
void dfs(long long now,int left){
if(left==0){
s.push(now);
while(!s.empty()){
long long a=s.top();
cout<<a<<" ";
s.pop();
}
}
if(check1(now)==1){
s.push(now);
mp[now*3]--;
// cout<<"check 1 "<<now*3<<" "<<left-1<<endl;
dfs(now*3,left-1);
// cout<<"not "<<now*3<<" "<<left-1<<endl;
mp[now*3]++;
s.pop();
}
if(check2(now)==1){
s.push(now);
mp[now/2]--;
// cout<<"check 2 "<<now/2<<" "<<left-1<<endl;
dfs(now/2,left-1);
// cout<<"not "<<now/2<<" "<<left-1<<endl;
mp[now/2]++;
s.pop();
}
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
mp[a[i]]++;
}
long long beg;
for(int i=1;i<=n;i++){
if((a[i]%3!=0 || !mp[a[i]/3]) && !mp[a[i]*2]){
beg=a[i];
break;
}
}
// cout<<beg<<endl;
dfs(beg,n-1);
return 0;
}