1038 Recover the Smallest Number (30分)[贪心算法][排序]

    技术2026-01-31  7

    By Jalan

    文章目录

    **By Jalan**知识工具需求数学数据结构和算法语言 题干输入条件输出条件例子例1输入输出 题解第一次思路预期时间复杂度编写用时代码CPP运行用时 结尾

    知识工具需求

    数学

    数据结构和算法

    语言

    string a; a.erase(index)可以去掉a中序号是index的那个字符.

    题干

    给一串数字,组个最大串出来.

    输入条件

    N<=10^4个数字 数字

    输出条件

    组成的最大的数

    例子

    例1

    输入

    5 32 321 3214 0229 87

    输出

    22932132143287

    题解

    第一次

    思路

    这题真的有30分? 比较一下吧,显然比较只需要A串和B串. 输出的时候注意把最前面的0都砍了,假如砍完没了害的记得输出个0; int v没有用上///也可以去了,比我想的简单一点.

    预期时间复杂度

    编写用时

    20分钟

    代码

    CPP

    #include <algorithm> #include <bits/stdc++.h> #include <stdio.h> #include <stdlib.h> #include <vector> using namespace std; typedef struct node { string s; int v; } node; vector<node> totalList; bool cmp(node a, node b) { return a.s + b.s < b.s + a.s; } int main(int argc, char const *argv[]) { //1 int n; scanf("%d", &n); totalList.resize(n); for (int i = 0; i < n; i++) { char temp[20]; scanf("%s", temp); totalList[i].s = temp; totalList[i].v = atoi(temp); } //2 sort(totalList.begin(), totalList.end(), cmp); //3 string ans; for (int i = 0; i < n; i++) { ans+=totalList[i].s; } while (ans.size()>0&&ans[ans.begin()-ans.begin()]=='0') { ans.erase(ans.begin()); } if (ans.size()!=0) { printf("%s",ans.c_str()); }else { printf("0"); } return 0; }
    运行用时

    结尾

    看在我写了这么多注释的份上可以给我点个赞嘛,求求惹=]砰砰砰,给我加点写下去的油呀 @.@ 也欢迎关注我的账号呀=]

    **开心code每一天**
    Processed: 0.019, SQL: 9