# Concatenate elements in an array into an integer and output the smallest integer

Source: Internet
Author: User

Merges the elements in an array into an integer and outputs the smallest integer. For example, the array {32,321}, the minimum is 32132, the array {100,22,199} has a minimum of 10019922 ...

This is a face test, or share my method. Because the merging of arrays may be out of bounds, the method of using integers is not feasible. My idea is to sort the array as needed, for example, before 32,321, 321, 32 after sorting. How about that sort of thing? In fact, two numbers are compared, the above 32 and 321 are connected to the number is 32132 and 32321, is to compare the two number who is small, who is in front. Of course, given the cross-border, processing is the use of strings.

Here's the code:

#include <iostream>
#include <sstream>
#define N 5
using namespace Std;

/**** compare two numbers connected to *****/

BOOL Sort_str (const string &str1,const string &str2)
{
BOOL flag = TRUE;
string str_temp1 = str1;
string str_temp2 = str2;
Str_temp1.append (STR2);
Str_temp2.append (STR1);

for (int j = 0;j<str_temp1.size (); ++j)
{
if ((str_temp1.at (j) -48) > (str_temp2.at (j)-48))
{
Flag = false;
Break
}

if ((str_temp1.at (j) -48) < (str_temp2.at (j)-48))
{
Flag = true;
Break
}

if ((str_temp1.at (j) -48) = = (Str_temp2.at (j)-48))
{
Continue
}
}
return flag;

}

/******* sorting an array **********/

void Array_sort (int *a,int N)
{

for (int i = 0;i<n-1;++i)
{
for (int j = 0;j<n-i-1;++j)
{
StringStream a_temp;
StringStream b_temp;
a_temp<<a[j];
b_temp<<a[j+1];
if (false = = Sort_str (A_temp.str (), B_temp.str ()))
{
int temp = A[j];
A[J] = a[j+1];
A[J+1] = temp;
}

}
}
}

int main ()
{
int A[n] = {32,321,3200,1999,3201};
Array_sort (A,n);

String Min_num;
StringStream temp;

/******** inserted into the string **********/
for (int i =0;i<n;++i)
{
StringStream temp;
temp<<a[i];
Min_num.append (Temp.str ());

}

cout<<min_num<<endl;

return 0;
}

Concatenate elements in an array into an integer and output the smallest integer

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.