正整数连接
题目描述:例如,3个整数32,3,21连接成的最小整数为:21323。
模板解释
:自定义了一个sort,
该程序定义的是 long long x[];
可以解决
该整数为int型范围内的问题,也就是数的位数为19位的问题
当然,你也可以设置int型或者unsigned long long来满足你的当前题意,或者节约空间。
示例程序:
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
long long x[500000];
//自定义的排序方法
//此模板可以解决x[]为多位时的数
bool compare_num(long long x, long long y)
{
long long temp_x = x;
long long temp_y = y;
long long len_x = 0;
long long len_y = 0;
while(temp_x>0)
{
temp_x = temp_x / 10;
len_x++;
}
while(temp_y>0)
{
temp_y = temp_y / 10;
len_y++;
}
return (x * powl(10, len_y) + y) < (y * powl(10, len_x) + x);
}
int main()
{
int i, j;
int n;
while(cin>>n)
{
for(i = 0; i < n; i++)
cin>>x[i];
sort(x, x + n, compare_num);//用自定的排序方法排序
for(i = 0; i < n; i++)
cout<<x[i];
cout << endl;
}
return 0;
}