Arrange an Array to Form a Smallest Digit



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 <iostream>
#include <stdlib.h>
#include <string.h>
#include <algorithm>

using namespace std;

int num_digit(int num)//转换成进制的位数
{
	int n=1;
	while(num != 0)
	{
		n *= 10;
		num /= 10;
	}
	return n;
}

bool camp(const int& a,const int& b)//比较a 和 b分别放在前面的大小,按从小到大排序
{
	return a * num_digit(b) + b < b * num_digit(a) + a;
}

int  smallestDigit(int a[],int nCount,char * strRst)
{
	sort(a,a + nCount,camp);
	char temp[100];
	for (int i = 0;i < nCount; i++)
	{
		sprintf(temp,"%d",a[i]);//将整型转换成字符串
		strcat(strRst,temp);//将temp 加到 strrst 的后面
	}
	return 0;
}

int mian()
{
	int a[] = { 4589, 101,41425,9999 };
	char szRst[100] = { 0 };
	smallestDigit(a, 4, szRst);
	cout << szRst;

	system("pause");
	return 0;
}


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. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值