PAT Advanced Level 1038

1038 Recover the Smallest Number (30)(30 分)

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 (<=10000) 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. Do not output leading zeros.

Sample Input:

5 32 321 3214 0229 87

Sample Output:

22932132143287

还是得相信锯响就有沫,水大泡倒墙。
/**********************
author: yomi
date: 18.5.27
ps: 可以说是走错了方向了 为什么正确的算法我就是想不到呢
答: 您做的题太少了
 啊 没脸看我写的东西 直接抄答案了 我会记住答案的技巧的
掉进了去前导零的大坑 中间为的零应该保留啊
这个脑子啊
一个大数 只去掉 最前面的零
而这个数是由多个数拼成的 必须先拼成那个数再去前导零啊
不然那么多的数 去哪个的啊
**********************/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 10010;
string str[maxn];
string ans = "";
int cmp(string a, string b)
{
    return a+b < b+a;
}
int main()
{
    int n;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> str[i];
    }
    sort(str, str+n, cmp);
    for(int i=0; i<n; i++){
        ans += str[i];
    }
    while(ans.size() && ans[0] == ‘0‘){///去前导零的好办法,要记住
        ans.erase(ans.begin());
    }
    if(!ans.size()){
        cout << "0";
    }
    else{
        cout << ans;
    }
    return 0;
}
/*   ------------21分--------------------
在抄答案的过程中恍然大悟 为什么我只有21分
我在输入的同时去掉了前导零 中间位的前导零是应该保留的
这样去掉了很多不该去掉的
21分是活该啊
struct str
{
    string value;
    char small;
}s[10010];
//char findsmall(string a)
//{
//    char ch1 = ‘A‘;
//    //cout << "test" <<endl;
//    for(int i=0; i<a.length(); i++){
//        if(a[i] < ch1){
//            ch1 = a[i];
//        }
//    }
//
//    return ch1;
//}
//int cmp(str a, str b)
//{
//    if(a.value[0]!=b.value[0])
//        return a.value<b.value;
//    return a.small<b.small;
//}
//int cmp(str a, str b)
//{
//    if(a.value[0]!=b.value[0])
//        return a.value < b.value;
//    if ((a.value+b.value)<(b.value+a.value)){
//        return a.value < b.value;
//    }
//    return a.value > b.value;
//
//}
int cmp(str a, str b)
{
    return a.value+b.value < b.value+a.value;
}
int main()
{
    int n;
    scanf("%d", &n);
    int flag = 1;
    for(int i=0; i<n; i++){
        cin >> s[i].value;
        int len = s[i].value.length();
        int pos = 0;
        for(int j=0; j<len; j++){
            if(s[i].value[j]!=‘0‘){
                break;
            }
            else{
                pos++;
            }
        }
        if(pos!=0)
            s[i].value = s[i].value.substr(pos, len);
        //s[i].small = findsmall(s[i].value);
    }
    sort(s, s+n, cmp);
    for(int i=0; i<n; i++){
        if(s[i].value.length()){
            cout << s[i].value;
            flag = 0;
        }

    }
    if(flag)
        cout << "0";
    return 0;
}
*/
/**
5 32 321 3214 0229 87

22932132143287
**/

原文地址:https://www.cnblogs.com/AbsolutelyPerfect/p/9096411.html

时间: 2024-11-09 05:53:11

PAT Advanced Level 1038的相关文章

PAT (Advanced Level) 1038. Recover the Smallest Number (30)

注意前导零的消去. #include <iostream> #include <string> #include <sstream> #include <algorithm> using namespace std; string s[10000+10]; int n; bool cmp(const string &a, const string &b) { return a+b<b+a; } int main() { cin>&

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi

Pat(Advanced Level)Practice--1016(Phone Bills)

Pat1016代码 题目描述: A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a lon

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c