简体   繁体   English

如何排列数字的数字,以便当我将其分成两个相同数字的数字时,两个数字的乘积最大?

[英]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.长话短说, xy彼此越近,它们的乘积就越大。

How to apply this to your problem?如何将此应用于您的问题?

  1. Take the largest digits in the set, assign them to the numbers (let them be A and B )取集合中最大的数字,将它们分配给数字(让它们成为AB
  2. As soon as one number becomes bigger than the other (let 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
Example #1示例#1

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

Example #2例子#2

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.

相关问题 检查这两个数字是否等于n位有效数字 - Check if the two numbers are equal to n significant digits 两个数字之间的总位数 - The total number of digits between two numbers 计算小于或等于N的两个数字的对数,以使对数的数字总和为质数 - Count number of pairs of two numbers less than or equal to N such that Sum of the digits of numbers of pair is Prime 找出前两位数字和后两位数字之和的平方等于数字本身的所有四位数字 - find all four digit numbers for which the square of the sum of the first two digits and the last two digits equal the number itself 数组中的最大数字,等于该数组中其他两个数字的乘积 - Maximum number from an array which is equal to product of two other numbers in that array 在数字上添加数字 - Adding numbers digits to number 在两个数字中寻找共享数字的麻烦 - Troubles with looking for shared digits in two numbers 比较两个数字的数字并求和时发出 - Issue while compare and sum digits of two numbers JAVA -我如何编写带有字母和数字的转换(小写-&gt;大写,单个数字-&gt;其值的两倍,两位数字-&gt;其数字总和)程序? - JAVA -How do I write a converting (lowercase->uppercase, single digits->double its value, two digits->sum of its digits) program w/ letters & numbers? 如何按数字的位数对列表中的数字进行分组 - How can I group numbers in a list by their number of digits
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM