[英]How to arrange the digits of a number such that when I break it into two numbers of equal digits the product of the two numbers is maximum?
Example: If I take the number 4544, then the maximum product I get is 2376 when I rearrange the digits as 5444. (54 x 44) = 2376 which is greater than (45 x 44) = 1980示例:如果我取数字 4544,那么当我将数字重新排列为 5444 时,我得到的最大乘积是 2376。(54 x 44) = 2376 大于 (45 x 44) = 1980
My approach for solving the problem was to permute the number in all possible ways, divide it into two parts and then find the max product from there.我解决这个问题的方法是用所有可能的方式排列数字,把它分成两部分,然后从那里找到最大的产品。 Is there a faster way to solve the problem or is a completely different approach to the problem is possible?
是否有更快的方法来解决问题,或者是否可以采用完全不同的方法来解决问题?
Here's my code:这是我的代码:
import java.util.*;
class MaxProduct
{
static int maxprod = 0;
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a no");
String n = sc.next();
permute(n,0);
System.out.println("Max Product is "+maxprod);
}
public static void permute(String s,int i)
{
if( i==s.length()-1)
{
int a = Integer.parseInt(s.substring(0,i/2+1));
int b = Integer.parseInt(s.substring(i/2+1));
maxprod = Math.max(a*b,maxprod);
}
else
{
for(int k=i;k<s.length();k++)
{
s =swapelement(s,k,i);
permute(s,i+1);
s=swapelement(s,k,i);
}
}
}
public static String swapelement(String a,int i, int j)
{
char x[]=a.toCharArray();
char ch = x[i];
x[i]=x[j];
x[j]=ch;
return String.valueOf(x);
}
}
Thank you!谢谢!
Let's look at a smaller problem first.我们先来看一个小问题。 For variables
x,y
, you are given对于变量
x,y
,你得到
x + y = c (constant)
The question is, when will x*y
be maximum?问题是,什么时候
x*y
最大? It can be solved in many ways, one of them being differential math .它可以通过多种方式解决,其中之一是微分数学。 Long story short, the closer
x
and y
are to each other, the larger their product is.长话短说,
x
和y
彼此越近,它们的乘积就越大。
A
and B
)A
和B
)A>B
), assign the remaining digits to maximise B
(larger digits in descending order).A>B
),分配剩余的数字以最大化B
(降序排列的较大数字)。 For A
we use the remaining smaller digits to make the largest number out of them.A
,我们使用剩余的较小数字从中得出最大的数字。 Assigning the larger digits to B
is done to make it closer to A
.B
是为了使其更接近A
。 Number = 123456 Taking largest digits, A = 6 and B = 5
. Number = 123456 取最大位数,
A = 6 and B = 5
。 Now, we will greedily maximise B
, so the numbers become A = 621 and B = 543
现在,我们将贪婪地最大化
B
,因此数字变为A = 621 and B = 543
Number = 88871256 Taking largest digits, A = 8 and B = 8
. Number = 88871256 取最大位数,
A = 8 and B = 8
。 Since they're still equal, we repeat step 1, so A = 88 and B = 87
.由于它们仍然相等,我们重复步骤 1,因此
A = 88 and B = 87
。 Now, we will greedily maximise B
, so the numbers become A = 8821 and B = 8765
.现在,我们将贪婪地最大化
B
,因此数字变为A = 8821 and B = 8765
。
Since this is a rather straightforward problem, adding any implementation seems unnecessary.由于这是一个相当简单的问题,因此添加任何实现似乎都是不必要的。
One approach can be two find two highest digits, and keeping them in the left most place and than do the opposite for remaining 2nd place.一种方法可以是两个找到两个最高数字,并将它们保留在最左边的位置,然后相反地保留第二位。
for example:例如:
if you have number 1234, the highest digits are 3 and 4 so lets keep them on first position, and for remaining 2 do the same so numbers will be:如果你有数字 1234,最高数字是 3 和 4,所以让他们先保留 position,剩下的 2 做同样的事情,所以数字将是:
(41*32) = 1312 (41*32) = 1312
so here you see 1312 is highest number possible所以在这里你看到 1312 是可能的最高数字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.