从C ++中的数字列表创建最大词典数字的程序
假设我们有一个称为nums的数字列表,我们必须重新排列其顺序以形成最大可能的数字并将其作为字符串返回。
因此,如果输入类似于nums=[20、8、85、316],则输出将为“88531620”。
为了解决这个问题,我们将遵循以下步骤-
定义阵列温度
对于我以数字表示的每个项目:
将我作为字符串插入temp
根据字典顺序对数组temp排序(当串联b大于b串联a时,检查两个字符串a,b)
对于临时中的每个字符串:
res:=res串联
返回资源
让我们看下面的实现以更好地理解-
示例
#include <bits/stdc++.h>
using namespace std;
static bool cmp(string a, string b) {
return (a + b) >= (b + a);
}
string solve(vector<int>& nums) {
vector<string> temp;
for (int i : nums) {
temp.push_back(to_string(i));
}
sort(temp.begin(), temp.end(), cmp);
string res;
for (string s : temp) {
res += s;
}
return res;
}
int main(){
vector<int> v = {20, 8, 85, 316};
cout << solve(v);
}输入值
{20, 8, 85, 316}输出结果
88531620
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短