题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805107638517760
#include <bits/stdc++.h>
using namespace std;
string ss;
string ans1, ans2;
set <int, greater
map<int, int> MAP;
int main()
{
cin >> ss;
ans1 = "int\[\] arr = new int\[\]{";
ans2 = "int\[\] index = new int\[\]{";
for ( int i = 0; i < ss.length(); i++ ) SET.insert(ss\[i\] - '0');
cout << ans1;
set<int>::iterator ite;
int total = SET.size();
int count = 0;
for ( ite = SET.begin(); ite != SET.end(); ite ++ )
{
cout << \*ite;
MAP\[\*ite\] = count;
count ++;
if ( count != total ) cout << ',';
}
cout << '}' << ';' << endl;
cout << ans2;
for ( int i = 0; i < ss.length(); i ++ )
{
cout << MAP\[ss\[i\] - '0'\];
if ( i != ss.length() - 1 ) printf(",");
}
cout << '}' << ';' << endl;
return 0;
}
set中默认从小到大排序,以下是从大到小排序的方案.
1 | set <int, greater<int>> SET; |