/**
* 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. 创建 elasticsearch 用户

    创建 elasticsearch 用户 groupadd -g 3048 elasticsearch useradd -s /sbin/nologin -u 3048 -g elasticsearch ...

  2. 微信公众号绑定服务器 Flask版

    python 代码 from flask import Flask, request from flask_cors import CORS app = Flask(__name__) app.app ...

  3. ECharts(中国地图)的使用 及 非空 tooltip formatter 验证

    中国地图使用频率很高下载文件:        echarts.min.js网址:               http://echarts.baidu.com/download.html点击:     ...

  4. js 报delete object in strict mode

    JAVA->Compiler->Building->No strictly compatible JRE for execution environment available Ig ...

  5. Dom4j工具类源码解析

    话不多说,上源码: package com.changeyd.utils;import java.io.File;import java.io.FileNotFoundException;import ...

  6. 攻击WEP加密无线网络

    1.介绍 针对客户端环境和无客户端环境下破解WEP的几类方法. 有客户端环境: 一般当前无线网络中存在活动的无线客户端环境,即有用户通过无线连接到无线AP上并正在进行上网等操作时. 无客户端环境: 1 ...

  7. C++内存管理(转)http://www.cnblogs.com/qiubole/archive/2008/03/07/1094770.html

    [导语] 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟的收获则是一遍一遍的检查代码和对C++的痛恨,但内存管理在C++中无处不 ...

  8. jsp中的JSTL与EL表达式用法

    JSTL (JSP Standard Tag Library ,JSP标准标签库) JSTL标签库分为5类:JSTL核心标签库.JSTL函数标签库.数据库标签库.I18N格式化标签库.XML标签库. ...

  9. text-decoration和text-indent和text-shadow

    text-decoration 属性规定添加到文本的修饰,规定划线的位置. <html> <head> <style type="text/css"& ...

  10. delphi-TTcpServer与TTcpClient

    最简单的TTcpServer与TTcpClient通信实例-Delphi_海盗船长_新浪博客http://blog.sina.com.cn/s/blog_5383794d0100nt9u.html d ...