• PAT 甲级 1038 Recover the Smallest Number


    https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704

    Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.

    Input Specification:

    Each input file contains one test case. Each case gives a positive integer N (≤) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print the smallest number in one line. Notice that the first digit must not be zero.

    Sample Input:

    5 32 321 3214 0229 87
    

    Sample Output:

    22932132143287

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn = 1e5 + 10;
    int N;
    
    struct Node{
        string s;
    }node[maxn];
    
    bool cmp(const Node& a, const Node& b) {
        return a.s + b.s < b.s + a.s;
    }
    
    int main() {
        scanf("%d", &N);
        for(int i = 0; i < N; i ++)
            cin >> node[i].s;
        sort(node, node + N, cmp);
        string ans = "";
        for(int i = 0; i < N; i ++)
            ans += node[i].s;
        int len = ans.length();
        int cnt = 0, temp = 0;
        for(int i = 0; i < len; i ++) {
            if(ans[i] != '0') {
                temp = i;
                break;
            }
            else cnt ++;
        }
        if(cnt == len)
            printf("0");
        else {
            for(int i = temp; i < len; i ++)
                cout << ans[i];
        }
        printf("
    ");
        return 0;
    }
    

    之前好像在  LeetCode 上做过类似的题目

  • 相关阅读:
    Two Sum II
    Subarray Sum
    Intersection of Two Arrays
    Reorder List
    Convert Sorted List to Binary Search Tree
    Remove Duplicates from Sorted List II
    Partition List
    Linked List Cycle II
    Sort List
    struts2结果跳转和参数获取
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10136805.html
Copyright © 2020-2023  润新知