【PAT甲级 - C++题解】1038 Recover the Smallest Number

简介: 【PAT甲级 - C++题解】1038 Recover the Smallest Number

1038 Recover the Smallest Number


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 (≤104) 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


题意

给定 N 个数字,他们可以随意进行组合成不同的数字,例如 {32,321,3214,0229,87} 可以组成 32-321-3214-0229-87 或 0229-32-87-321-3214 等,但所有组合中 0229-321-3214-32-87 得到的数字是最小的。


现在我们需要找到这 N 个数字组合后最小的那个数字。


思路

这题我们可以发现一个规律,可以通过判断拼接后的字符串的大小来进行排序,假定有字符串 x 和 y :


如果 x+y > y+x ,则 x “大于” y ,即 x 排在 y 的后面。

如果 x+y < y+x ,则 x “小于” y ,即 x 排在 y 的前面。

注意: 上述的加法运算并不是实际意义上的整数加法运算,而是字符串的拼接。当然,大于小于也不是整数的大小比较,而是指排序的规则。


例如 x = 3 且 y = 32 ,可以发现:


x + y = 332 > y + x = 323 ,所以我们认为 x “大于” y ,即 x 要排在 y 的后面。

故通过上述规则对数组中的所有字符串进行排序,然后再将排序后的数组中的字符串拼接起来得到的字符串一定是最小的。


代码

#include<bits/stdc++.h>
using namespace std;
const int N = 10010;
string a[N];
int n;
int main()
{
    //输入每个值
    cin >> n;
    for (int i = 0; i < n; i++)    cin >> a[i];
    //按规则进行排序
    sort(a, a + n, [](string& a, string& b) {return a + b < b + a; });
    //合成字符串
    string res = "";
    for (int i = 0; i < n; i++)    res += a[i];
    //去掉前缀0
    int k = 0;
    while (k + 1 < res.size() && res[k] == '0')   k++;
    //输出结果
    cout << res.substr(k) << endl;
    return 0;
}
目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
59 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
76 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
70 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
68 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
74 0
|
10天前
|
编译器 C++
C++ 类构造函数初始化列表
构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式。
56 30
|
24天前
|
存储 编译器 C++
C ++初阶:类和对象(中)
C ++初阶:类和对象(中)
|
1月前
|
存储 安全 编译器
【C++】类和对象(下)
【C++】类和对象(下)
【C++】类和对象(下)
|
24天前
|
C++
C++(十六)类之间转化
在C++中,类之间的转换可以通过转换构造函数和操作符函数实现。转换构造函数是一种单参数构造函数,用于将其他类型转换为本类类型。为了防止不必要的隐式转换,可以使用`explicit`关键字来禁止这种自动转换。此外,还可以通过定义`operator`函数来进行类型转换,该函数无参数且无返回值。下面展示了如何使用这两种方式实现自定义类型的相互转换,并通过示例代码说明了`explicit`关键字的作用。
|
24天前
|
存储 设计模式 编译器
C++(十三) 类的扩展
本文详细介绍了C++中类的各种扩展特性,包括类成员存储、`sizeof`操作符的应用、类成员函数的存储方式及其背后的`this`指针机制。此外,还探讨了`const`修饰符在成员变量和函数中的作用,以及如何通过`static`关键字实现类中的资源共享。文章还介绍了单例模式的设计思路,并讨论了指向类成员(数据成员和函数成员)的指针的使用方法。最后,还讲解了指向静态成员的指针的相关概念和应用示例。通过这些内容,帮助读者更好地理解和掌握C++面向对象编程的核心概念和技术细节。