/**
* 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. mySql插入网页地址失败

    如题:插入的网页地址失败,只显示了开头10位字符.以为是特殊字符的问题. 后来发现是字符串长度不够: ALTER TABLE `news`  MODIFY COLUMN `from` VARCHAR( ...

  2. Study 6 —— 字体和段落属性

    字体风格{font-style:normal | italic | oblique | inherit字体复合属性{font:font-style font-variant font-weight f ...

  3. 出栈顺序 与 卡特兰数(Catalan)的关系

    一,问题描述 给定一个以字符串形式表示的入栈序列,请求出一共有多少种可能的出栈顺序?如何输出所有可能的出栈序列? 比如入栈序列为:1 2 3  ,则出栈序列一共有五种,分别如下:1 2 3.1 3 2 ...

  4. ECSHOP /api/client/includes/lib_api.php

    ecshop /api/client/api.php./api/client/includes/lib_api.php ECShop存在一个盲注漏洞,问题存在于/api/client/api.php文 ...

  5. This page is about building Firefox Desktop

    This page is about building Firefox Desktop The Mozilla build system, like the rest of the Mozilla c ...

  6. tensorflow实现mnist

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 在变量的构建时,通过trunc ...

  7. android 使用get和post将数据提交到服务器

    1.activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android& ...

  8. 如何利用 Jmeter 测试上传文件

    在查看本文的前提,有如下几点: 会使用Jmeter,知道如何去添加http请求的sampler 会利用Jmeter完成基本的http请求或者是接口测试 知道文件上传是怎么回事 言归正传,其实文件上传我 ...

  9. tcp的连接断开

    tcp的断开连接是需要主机完成四次挥手的过程的,并不是断网了就表示断开连接了.假如双方已经建立起了连接,突然一方断网(比如突然停电,或者网线突然被拔了),对于另一方来讲他并不会知道这个情况,他依然认为 ...

  10. python - classs内置方法 solt

    solt # __solt__ # 是一个类变量,变量值可以是列表.元组或者是可迭代对象,也可以是一个字符串 # (以为这所有实例只有一种数据属性) # # 作用:(为了节省内存空间,减少过多的实例属 ...