PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)

简介: PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)

题目链接:点击打开链接

题目大意:略。

解题思路:其实就是一个序的关系,所有的组合有n!种,(像"所谓组出最小数其实是获得字典序最小的拼接方式"这种废话我就不说了)。假设我们获得了其中的一个组合,然后又两个相邻的数字片段a,b。然后我们就要想,把a和b交换能不能使整个序列变小呢?这个问题的其实等价于b+a 是否小于a+b(此处"+"为连接符),也就是说对于这样一个序列,如果某两个相邻的元素之间发生交换可以使得整个序列的值变小,我们就应该坚决的交换。

这样一来,比较每两个相邻的元素,如果交换可以使得整个序列变大,就交换之,直到最后没有任何两个值之间能进行交换,啊,这不就是传说中的Bubble_Sort吗,真是一个令人激动的结论啊。而C++ sort函数就可以满足此要求。


AC 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a)
#define ssclr(ss) ss.clear(), ss.str("")
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
const int maxn=1e4+10;
int cmp(string a,string b)
{
    return a+b<b+a;
}
int main()
{
    int n,p=0;
    string srr[maxn], rs;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        cin>>srr[i];
    sort(srr,srr+n,cmp);
    for(int i=0;i<n;i++) rs.append(srr[i]);
    int len=rs.length();
    while(p<len && rs[p]=='0') p++;
    if(p==len) puts("0");
    else printf("%s\n",rs.c_str()+p);
    return 0;
}
目录
相关文章
|
C++
【PAT甲级 - C++题解】1038 Recover the Smallest Number
【PAT甲级 - C++题解】1038 Recover the Smallest Number
48 1
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
55 0
|
C++ 容器
【PAT甲级 - C++题解】1144 The Missing Number
【PAT甲级 - C++题解】1144 The Missing Number
45 0
|
存储 人工智能 C++
【PAT甲级 - C++题解】1104 Sum of Number Segments
【PAT甲级 - C++题解】1104 Sum of Number Segments
57 0
|
人工智能 文件存储 C++
【PAT甲级 - C++题解】1019 General Palindromic Number
【PAT甲级 - C++题解】1019 General Palindromic Number
56 0
|
C++
【PAT甲级 - C++题解】1024 Palindromic Number
【PAT甲级 - C++题解】1024 Palindromic Number
28 0
|
C++
【PAT甲级 - C++题解】1082 Read Number in Chinese
【PAT甲级 - C++题解】1082 Read Number in Chinese
70 0
PAT (Advanced Level) Practice - 1082 Read Number in Chinese(25 分)
PAT (Advanced Level) Practice - 1082 Read Number in Chinese(25 分)
85 0
|
7月前
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
72 1
|
16天前
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】