其实我加这个答案只是为了分享另一种方法,除此之外没有别的。
约束。
1 <=N <=1000
0 <= ar[i] <= 1000
第一行N表示一个数组中的元素数。
和第二行由数组元素组成
例子。
2
100, 9
产量。9100
例2:
3
12, 13, 8
产量。81312
这段代码对于大数是行不通的,而且在时间复杂度上也不是一个有效的解决方案。
由于一些误解我的帖子被删除了请大家考虑一下这是我的问题和我的解决方案只是由于有人建议编辑将答案加到了问题中作为 "我的代码到目前为止:"
import java.io.*;
import java.util.*;
public class Solution
{
static long ans=0;
public static void main(String[] args)
{
int N,count=0;
long n;
Scanner scan=new Scanner(System.in);
N=scan.nextInt();
long[] ar=new long[N];
long[] vis=new long[N];
for(int i=0;i<N;i++)
{
ar[i]=scan.nextLong();
vis[i]=0;
n=ar[i];
while(n!=0)
{
n/=10;
count++;//It counts the number of digits of all elements in the given array
}
}
int index=0;
long val=0,a=0;
int x=0;
recur(ar,vis,index,N,count,val,a);
System.out.println(ans);
}
static void recur(long ar[],long vis[],int index,int N,int count,long val,long a)
{
if(index==N)
{
ans=Math.max(ans,val);
return;
}
for(int i=0,j=0;i<N;i++,j++)
{
if(vis[i]==0)
{
vis[i]=1;
a=counter(ar[i]);//counter returns no. of digits in the current element
count-=a;//this returned value is subtracted from the count(which is the number of digits of all elements in the array)
val+=ar[i]*(Math.pow(10,count));// now the corresponding digit's place is multiplied with the element to make up the number
recur(ar,vis,index+1,N,count,val,a);
vis[i]=0;
val-=ar[i]*(Math.pow(10,count));
count+=a;
}
}
}
static long counter(long val)
{
int count=0;
while(val!=0)
{
val/=10;
count++;
}
return count;
}
}