/**
* Input an array of positive integers, arrange the integers to form new digits,
* and output the smallest digit among all the new ones.
* Input Example 1:
* {2, 1}
* Output Example 1:
* 12
*
* Input Example 2:
* {32, 321}
* Output Example 2:
* 32132
*
* Input Example 3:
* {4589, 101,41425,9999}
* Output Example 3:
* 1014142545899999;
*/ // 功能:将输入的数组排成最小的数
// 输入: int a[]:整型数组
// int nCount:数组长度
// char * strRst 返回值
// 输出:
// 返回:成功返回0 异常返回-1 #include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm> using namespace std; bool cmp(string str1, string str2)
{
return (str1 + str2 < str2 + str1);
} int smallestDigit(int a[], int nCount, char *strRst)
{
if(NULL == a || nCount <= 0 || NULL == strRst)
{
return -1;
} vector<string> numStrs;
stringstream strStream;
string num;
/*! int转string */
for(int i = 0; i < nCount; ++i)
{
strStream << a[i];
strStream >> num;
strStream.clear();
numStrs.push_back(num);
} sort(numStrs.begin(), numStrs.end(), cmp); int len = 0;
for(int i = 0; i < numStrs.size(); ++i)
{
len = numStrs[i].size();
strncpy(strRst, numStrs[i].c_str(), len);
strRst += len;
}
*strRst = '\0'; return 0;
} int main()
{
int a[] = {4589, 101, 41425, 9999};
char buf[1000];
smallestDigit(a, 4, buf);
cout << buf << endl; return 0;
}

Arrange an Array to Form a Smallest Digit的更多相关文章

  1. 5403. Find the Kth Smallest Sum of a Matrix With Sorted Rows

    You are given an m * n matrix, mat, and an integer k, which has its rows sorted in non-decreasing or ...

  2. [LeetCode] Find K-th Smallest Pair Distance 找第K小的数对儿距离

    Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...

  3. [Swift]LeetCode719. 找出第 k 小的距离对 | Find K-th Smallest Pair Distance

    Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...

  4. [Swift]LeetCode908. 最小差值 I | Smallest Range I

    Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...

  5. [Swift]LeetCode910. 最小差值 II | Smallest Range II

    Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and ad ...

  6. LeetCode 908 Smallest Range I 解题报告

    题目要求 Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K ...

  7. [LeetCode] 561. Array Partition I_Easy tag: Sort

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  8. [LeetCode&Python] Problem 908. Smallest Range I

    Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...

  9. 解题报告-908. Smallest Range I

    题目 : Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K ...

随机推荐

  1. Problems you may meet

    一.正确安装sklearn却提示No module named 'sklearn.lda' It seems that you have installed a newer version of sk ...

  2. Golang面向对象编程-struct(结构体)

    Golang面向对象编程-struct(结构体) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是面向对象编程 面向对象编程(Object Oriented Program ...

  3. 在tomcat集群环境下redis实现分布式锁

    上篇介绍了redis在集群环境下如何解决session共享的问题.今天来讲一下如何解决分布式锁的问题 什么是分布式锁? 分布式锁就是在多个服务器中,都来争夺某一资源.这时候我们肯定需要一把锁是不是 , ...

  4. Hbase记录-HBaseAdmin类

    HBaseAdmin是一个类表示管理.这个类属于org.apache.hadoop.hbase.client包.使用这个类,可以执行管理员任务.使用Connection.getAdmin()方法来获取 ...

  5. bootstrap3 input 验证样式【转】

    feedback <form role="form"> <div class="form-group has-success has-feedback& ...

  6. UVALive 7143 Room Assignment(组合数学+DP)

    题目链接 参考自:http://www.cnblogs.com/oyking/p/4508260.html 题意 n个人,其中有k对双胞胎.现有m间房间,每间房间有容量ci问分配房间的方案数. 分析 ...

  7. Uva439:BFS题目总结

    #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstring> #include <cstd ...

  8. UpnP Hacking

    1.概述 通用即插即用(UpnP)是一种用于PC机和智能设备的常见对等网络连接的体系结构. UPnP以Internet标准和技术(例如Tcp/IP.HTTP和XML)为基础,使这样的设备彼此可自动连接 ...

  9. Hadoop mapreduce执行过程涉及api

    资源的申请,分配过程略过,从开始执行开始. mapper阶段: 首先调用默认的PathFilter进行文件过滤,确定哪些输入文件是需要的哪些是不需要的,然后调用inputFormat的getSplit ...

  10. UE4中Bebavior Tree中Delay及其后面代码失效的原因

    具体原因是因为节点的执行过程中,该节点及其父节点的Decorator条件不满足,而节点又受到flow control的影响,导致中途强制结束了Task节点的执行,具体如下. UE4中的Behavior ...