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. int nCount: length of the array char * strRst: returned value |
代码如下:
#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. int nCount: length of the array char * strRst: returned value |