1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#include <stdio.h> #include <iostream> #include <vector> #include <string> #include <cstring>
using namespace std; bool check[100001]; int ans = 0; int arr[100001]; vector<int> value_v;
bool compare(const string &a, const string b){ if(b.size() > a.size()){ return b > a; }else { return b < a; } } void dfs(int idx,vector<int> v,int len){ if(idx == len){ string str = ""; for(int i=0; i<len; i++){ str += to_string(value_v[i]);
} int comp = atoi(str.c_str()); if(comp > ans){ ans = comp; } return; } for(int i=0; i<len; i++){ if(check[i]){ continue; } check[i] = true; value_v.push_back(v[i]); dfs(idx+1, v, len); value_v.pop_back(); check[i] = false; } } int main(void){ vector<string> s; s.push_back("3"); s.push_back("30"); s.push_back("34");s.push_back("5");s.push_back("9"); int len = s.size(); vector<int> v(len); sort(s.begin(), s.end(), compare); for(int i=0; i<s.size(); i++){ v[i] = atoi(s[i].c_str()); } dfs(0,v,len); cout << to_string(ans); return 0; }
|