/**
* 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. python爬虫爬取页面源码在本页面展示

    python爬虫在爬取网页内容时,需要将内容连同内容格式一同爬取过来,然后在自己的web页面中显示,自己的web页面为django框架 首先定义一个变量html,变量值为一段HTML代码 >&g ...

  2. pyautogui_pdf批量转换为TXT

    pyautogui_pdf批量转换为TXT, 用pdf自带无损转换 # -*- coding: utf-8 -*- """ Created on Thu May 5 15 ...

  3. MySQL_DDL(不定时更新)

    1. //1.创建数据库,并指定字符集为utf8 create database rocker_oa default character set utf8; //2.创建用户,并指定密码为‘root’ ...

  4. Scala进阶之路-反射(reflect)技术详解

    Scala进阶之路-反射(reflect)技术详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Scala中的反射技术和Java反射用法类似,我这里就不一一介绍反射是啥了,如果对 ...

  5. 函数和常用模块【day06】:shelve模块(五)

    本节内容 1.简述 2.shelve概念 3.shelve模块使用 4.总结 一.简述 之前我们说不管是json也好,还是pickle也好,在python3中只能dump一次和load一次,不能dum ...

  6. AES加密【转】

    .   此时就一定要使用如下代码步骤 : 1.SecureRandom的key定下来. SecureRandom 实现完全隨操作系统本身的內部狀態,除非調用方在調用 getInstance 方法之後又 ...

  7. Dojo框架:误解与现实[转载]

    随着Ajax技术的流行,越来越多的Web应用使用Ajax技术来提高用户体验.使用Ajax技术的一个重要优势是不需要额外的浏览器插件支持,只需要使用浏览器原生的API,并利用JavaScript来操作即 ...

  8. IPv4套接字地址结构

    一.IPv4套接字地址结构(POSIX定义) (1)长度字段sin_len是为增加对OSI协议的支持而随4.3BSD-Reno添加的:并不是所有的厂家都支持套接字地址结构的长度字段,而且POSIX规范 ...

  9. SSM-1第一章 认识SSM框架和Redis

    第一章 认识SSM框架和Redis   1.1 Spring框架 Sring理念  Ioc  控制反转  IOC是一个容器,在Spring中,它会认为一切Java资源都是JavaBean,容器的目标是 ...

  10. POJ1287 Networking【最小生成树】

    题意: 给出n个节点,再有m条边,这m条边代表从a节点到b节点电缆的长度,现在要你将所有节点都连起来,并且使长度最小 思路: 这是个标准的最小生成树的问题,用prim的时候需要注意的是他有重边,取边最 ...