Arrange an Array to Form a Smallest Digit
来源:互联网 发布:阿里巴巴有淘宝客吗 编辑:程序博客网 时间:2024/05/21 13:58
描述:
Question:
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;
Interface:
int smallestDigit(int a[],int nCount,char * strRst)
Function: Arrange digits in the input array to form a smallest digit.
Input: int a[]: an array of integers
int nCount: length of the array
char * strRst: returned value
Output: none
Return: o indicates success and -1 indicates exception.
#include "oj.h"#include <stdlib.h>#include <string.h>#include <stdio.h>#include <iostream> #include <sstream> using namespace std;// 功能:将输入的数组排成最小的数// 输入: int a[]:整型数组// int nCount:数组长度// char * strRst 返回值// 输出:// 返回:成功返回0 异常返回-1int myCompare(const void *str1 , const void *str2){char *s1 = new char[20]();char *s2 = new char[20]();strcpy(s1 , *(const char **)str1);strcat(s1 , *(const char **)str2);strcpy(s2 , *(const char **)str2);strcat(s2 , *(const char **)str1);int result = strcmp(s1 , s2);delete [] s1;delete [] s2;return result;}int smallestDigit(int a[],int nCount,char * strRst){if(!a || nCount<=0)return -1;int i;char **strNumbers = (char **)(new int[nCount]());for(i=0; i<nCount; i++) { strNumbers[i] = new char[10]();sprintf(strNumbers[i] , "%d" , a[i]);} qsort(strNumbers, nCount , sizeof(char *) , myCompare);for(i=0; i<nCount; i++) {strcat(strRst , strNumbers[i]);}for(int i = 0 ; i < nCount ; i++)delete [] strNumbers[i];delete [] strNumbers; return 0;}
0 0
- Arrange an Array to Form a Smallest Digit
- Arrange an Array to Form a Smallest Digit
- Arrange an Array to Form a Smallest Digit
- Arrange an Array to Form a Smallest Digit
- [华为机试练习题]29.Arrange an Array to Form a Smallest Digit
- 华为机试题:Arrange an Array to Form a Smallest Digit
- Given an array of characters which form a sentence of words, give an efficient algorithm to reverse
- Resizing a Form to Fit an Image
- CareerCup Given an array of (unsorted) integers, arrange them such that a < b > c < d > e... etc.
- Converting a array to an array of ranges in ruby
- find ith smallest element in an array
- [leetcode]Kth Smallest Element in an Array
- A very efficient and economical way to shift an array
- Algorithm: Reverse a Integer to an array with recursion
- How to group anagrams in a string into an array
- pointer to ref of an array, and to the func that return a ref of an array
- 001:Why is it faster to process a sorted array than an unsorted array?
- Why is it faster to process a sorted array than an unsorted array?
- Redis常用命令
- Android之Handler用法总结
- android-Copy and Paste(text and input)
- Activity生命周期
- Java Executor 框架学习总结
- Arrange an Array to Form a Smallest Digit
- Spark 学习视频汇总
- 【c/c++】delete和delete[]详解
- mysql 版本
- Win7系统怀疑重启
- 设计模式系列(1)---观察者模式解析与应用
- eclipse 一直loading org.hibernate.eclipse解决方法
- JS检测用户输入密码强度代码
- iOS开发项目中常用到的一些宏