Source:

PAT A1038 Recover the Smallest Number (30 分)

Description:

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 Nnumber 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

Keys:

  • 贪心

Code:

 /*
Data: 2019-07-23 18:47:04
Problem: PAT_A1038#Recover the Smallest Number
AC: 16:22 题目大意:
给几个数,求拼成的最小数
*/
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
const int M=1e4+;
string s[M]; bool cmp(string a, string b)
{
return a+b < b+a;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n;
scanf("%d", &n);
for(int i=; i<n; i++)
cin >> s[i];
sort(s,s+n,cmp);
string ans="";
for(int i=; i<n; i++)
ans += s[i];
while(ans.size()> && ans[]=='')
ans.erase(,);
cout << ans; return ;
}

PAT_A1038#Recover the Smallest Number的更多相关文章

  1. 把数组排成最小的数/1038. Recover the Smallest Number

    题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323.   Give ...

  2. 1038. Recover the Smallest Number (30) - 字符串排序

    题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...

  3. A1038. Recover the Smallest Number

    Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...

  4. PAT甲1038 Recover the smallest number

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  5. 1038 Recover the Smallest Number (30 分)

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  6. 1038. Recover the Smallest Number (30)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...

  7. PAT 1038 Recover the Smallest Number[dp][难]

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  8. PAT 甲级 1038 Recover the Smallest Number

    https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 Given a collection of ...

  9. pat1038. Recover the Smallest Number (30)

    1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

随机推荐

  1. Chosen 的 optgroup 第一级单击的时候选择二级的全部

    相关环境 及 版本 Chosen (v1.6.2) https://harvesthq.github.io/chosen/ jQuery (v1.8.3) 官网 http://jquery.com/ ...

  2. linux卸载

    centos平台 yum remove xxxrpm包,rpm -e xxxtar包直接删除该文件或者make uninstall xxx

  3. angularJS CDN

    http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js

  4. spring boot MVC

    1 spring boot的用途 第一,spring boot可以用来开发mvc web应用. 第二,spring boot可以用来开发rest api. 第三,spring boot也可以用来开发w ...

  5. 【模板】fread读入优化 & fwrite输出优化

    #include <iostream> #include <cstdio> #include <cctype> #define SIZE (1 << 2 ...

  6. Python值正则表达式(RE)

    要想在Python中使用正则表达式,首先要引入模块: import re . 匹配任意一个 +   匹配至少一个 * 匹配0个至多个 ? 1个或0个(可有可无) - 表范围 \ 转义 ^   在首 $ ...

  7. spring源码解读之 JdbcTemplate源码

    原文:https://blog.csdn.net/songjinbin/article/details/19857567 在Spring中,JdbcTemplate是经常被使用的类来帮助用户程序操作数 ...

  8. MyEclipse中android 项目如何解决第三方jar无法关联源码的问题( The JAR of this class file belongs to container 'Android Private Libraries' which does not allow modifications to source attachments on its entries.)

    若我们要为第三方jar(android-support-v4.jar)关联源码通常的做法是 右键项目 单击菜单Properties 单击菜单 Java Build Path 单击 Libraries ...

  9. robotframework+python3+selenium之web相关关键字---第二集

    1.F5可查看所有关键字,如图: 2.浏览器相关关键字: 2.1  Open Browser    https://www.baidu.com  chrome    # 打开浏览器,rf默认使用火狐浏 ...

  10. JSP中调用Spring的方法

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...