其他分享
首页 > 其他分享> > PAT (Advanced Level) Practice 1038 Recover the Smallest Number (30 分) 凌宸1642

PAT (Advanced Level) Practice 1038 Recover the Smallest Number (30 分) 凌宸1642

作者:互联网

PAT (Advanced Level) Practice 1038 Recover the Smallest Number (30 分) 凌宸1642

题目描述:

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.

译:规定你个一个数字序列集合,,您应该从中恢复最小的数字。 例如,给定 { 32, 321, 3214, 0229, 87 },我们可以恢复许多数字,例如 32-321-3214-0229-87 或 0229-32-87-321-3214 相对于不同的组合顺序 这些段,最小的数字是 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.

译:每个输入文件包含一个测试用例。 每个 case 给出一个正整数 N (≤104),后跟 N 个数字段。 每个段包含一个不超过 8 位的非负整数。 一行中的所有数字都用空格分隔。


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

The Idea:

The Codes:

#include<bits/stdc++.h>
using namespace std ;
bool cmp(string s1 , string s2){
	return s1 + s2 < s2 + s1 ; 
}
int main(){
	int n ;
	cin >> n ;
	vector<string> ss(n) ;
	for(int i = 0 ; i < n ; i ++) cin >> ss[i] ;
	sort(ss.begin() , ss.end() , cmp) ;
	int temp = stoi(ss[0]) ; // 去除第一个数字的前导零 如 0229 --> 229 
	string ans = to_string(temp) ;
	for(int i = 1 ; i < n ; i ++){
		temp = stoi(ss[i]) ;  
		if(temp != 0)ans += ss[i] ; // 答案输出为 0 时,避免输出多个0  
	}
	cout << ans << endl ;
	return 0 ;
}

/*
	PAT 的测试数据中应该不存在类似于下述这种测试数据:
 		
 		in:		3 00 010 001
 			
*/ 

标签:3214,0229,PAT,数字,Level,32,30,321,87
来源: https://www.cnblogs.com/lingchen1642/p/15168737.html