1038. Recover the Smallest Number

来源:互联网 发布:原知宏大野智 编辑:程序博客网 时间:2024/05/18 00:10

比较喜欢这种题,代码不长,但是有意思。

从别人那学习到的

#include <iostream>#include <string>#include <algorithm>#include <stdio.h>using namespace std;bool compare(const string &str1, const string &str2){    return str1+str2 < str2+str1;}int main(){    int n;    string *nums;    cin >> n;    nums = new string[n];    for(int i = 0; i < n; i++)        cin >> nums[i];    sort(nums, nums+n, compare);    string output="";    for(int i = 0; i < n; i++)        output += nums[i];    int i;    for(i = 0; i < output.length()-1; i++)    {        if(output[i] != '0')            break;    }    output = output.substr(i);    cout << output;    return 0;}


0 0
原创粉丝点击