【问题标题】:How do I rearrange an integer so that it is rearranged as its highest possible value in Java? [duplicate]如何重新排列一个整数,以便将其重新排列为 Java 中的最高可能值? [复制]
【发布时间】:2021-01-16 14:34:47
【问题描述】:

例如,如果有人插入 34603,则输出将是 64330。我已经开始解决这个问题,但我想不出一个可行的解决方案。另外,由于这是一项作业,我的导师告诉我不允许使用数组。到目前为止,这是我所拥有的:

public class loops{
    loops(){}
    public void biggest(int a){
        String as = Integer.toString(a);
        int index=0;
        int asl = as.length();
        while(index<asl){
            String num1 = as.substring(index);
            String num2 = as.substring((index+1));
            int con1 = Integer.parseInt(num1);
            int con2 = Integer.parseInt(num2);
        
               if(con1<con2){
                   System.out.println("con2: "+con2);
               }
               if(con1>con2){
                   System.out.println("con1: "+con1);
               }
               
            System.out.println("added: "+con1+" "+con2);
            
            index++;
        }
    }

     public static void main(String []args){
        loops x = new loops();
        x.biggest(4583);        
     }
}

我将不胜感激任何和所有的帮助/提示,​​因为我真的迷失了这个。

【问题讨论】:

标签: java algorithm loops methods


【解决方案1】:

应该很明显,最大可能的结果是通过按数字降序排列来获得的。一种更简单、更有效的方法是计数排序,但通常的形式涉及使用数组或等效数组来累积计数。

因此,标准计数排序以及旨在重新排列项目序列的所有标准排序例程都已淘汰。但是您仍然可以从计数排序中获得灵感。例如,找出输入中有多少个 9,并从这多个 9 中形成一个数字。然后计算出有多少个 8 并附加它们。那么有多少个 7,。将数字“附加”到数字可以通过算术方式完成,因此整个过程可以在没有数组或数组等价物的情况下完成,即使我们认为字符串是数组等价物(我们应该这样做)。

细节保留原样。

【讨论】:

    【解决方案2】:

    我不会直接为你回答这个问题,但会提出一些可以帮助你的想法。

    1. 您需要在适当的位置对整数进行排序 - 即没有数组/没有列表.. 只需将整数作为您正确执行的字符串进行迭代,并逐步交换值,以便最终得到排序后的数值。

    2. 考虑各种排序算法、快速排序、归并排序、冒泡排序等。您可以有效地选择其中一种算法并尝试实现它。

    3. 从整数排序的基本示例开始,并迭代开发代码以连续生成正确答案...作为测试用例尝试:

      1. 根本没有数字...null
      2. 然后是空字符串""
      3. 然后是一个数字,所以一个数字0-9
      4. 接下来的两位数字都按顺序排列,然后不按顺序排列
      5. 然后是 3 位数字输入/输出顺序

      实现 3 位数后,您应该能够生成任意位数的解决方案。

    注意:如果您使用Integer 作为输入数据类型,您将被限制为能够采用Integer.MAX_VALUE 的最大整数值(不是那么大)。尝试将输入参数视为 String 并将各个数字视为整数进行比较(您已经在这样做),这样您就可以处理更大的输入。

    【讨论】:

      【解决方案3】:

      导入 java.util.ArrayList; 导入 java.util.Collections;

      公共类循环{ 循环(){}

      public void biggest(int a){
          String as = Integer.toString(a);
          int index=0;
          int index2=1;
          int asl = as.length();
          ArrayList<Integer> lista = new ArrayList<Integer>();
          
          while(index<asl){
            String num1 = as.substring(index,index2);
            int con1 = Integer.parseInt(num1);
            lista.add(con1);
            index++;
            index2++;
          }
          //order list
          Collections.sort(lista);
          Collections.reverse(lista);
          System.out.println(lista);
          //concatenate numbers
      
      }
      
       public static void main(String []args){
          loops x = new loops();
          x.biggest(34603);        
       }
      

      }

      **任何事情都请回来咨询。 **

      【讨论】:

        【解决方案4】:

        好的,我想出了这个。这不是最漂亮的解决方案,我承认,但它确实有效。看看:

        public class loops{
            public int a,b,c,d,e,f,g,h,i,j;
            public loops(){}
        
        public void biggest(int a){
                String as = Integer.toString(a);
                int index=0;
                a = 0;
                b = 0;
                c = 0;
                d = 0;
                e = 0;
                f = 0;
                g = 0;
                h = 0;
                i = 0;
                j = 0;
                int asl = as.length();
                while(index<asl){
                    String num3 = as.substring(index,(index+1));
                    int con3 = Integer.parseInt(num3);
                    if(con3==9){a++;}
                    if(con3==8){b++;}
                    if(con3==7){c++;}
                    if(con3==6){d++;}
                    if(con3==5){e++;}
                    if(con3==4){f++;}
                    if(con3==3){g++;}
                    if(con3==2){h++;}
                    if(con3==1){i++;}
                    if(con3==0){j++;}
                       index++;
                    }
        
                 for(int z=0;z<a;z++){
                  System.out.print("9");
                  }
                  for(int y=0;y<b;y++){
                  System.out.print("8");
                  }
                  for(int x=0;x<c;x++){
                  System.out.print("7");
                  }
                  for(int w=0;w<d;w++){
                  System.out.print("6");
                  }
                  for(int v=0;v<e;v++){
                  System.out.print("5");
                  }
                  for(int u=0;u<f;u++){
                  System.out.print("4");
                  }
                  for(int t=0;t<g;t++){
                  System.out.print("3");
                  }
                  for(int s=0;s<h;s++){
                  System.out.print("2");
                  }
                  for(int r=0;r<i;r++){
                  System.out.print("1");
                  }
                  for(int q=0;q<j;q++){
                  System.out.print("0");
                  }
        public static void main(String []args){
                loops x = new loops();
                x.biggest(45683408); 
             }
           }
        

        【讨论】:

          【解决方案5】:

          如果不允许使用数组,那么我认为也不应该允许使用字符串:

          static void biggest(int n)
          {
              long counts=0;
              for(; n>0; n/=10)
              {
                  counts += 1L<<((n%10)*4);
              }
              long result=0;
              for (long digit=9; digit>=0; --digit)
              {
                  for(long rep=(counts>>(digit*4))&15; rep>0; --rep)
                  {
                      result = result*10 + digit;
                  }
              }
              System.out.println(result);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-02-24
            • 2015-09-02
            • 1970-01-01
            • 2018-11-27
            相关资源
            最近更新 更多