java 根据因子数对整数数组进行排序(Zoho布局问题)

qvsjd97n  于 2023-04-10  发布在  Java
关注(0)|答案(1)|浏览(115)
import java.util.Scanner;

public class DecreasingIntAccToFactors {

    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        // Static Way of Array Creation
        // int[] a = { 3, 4, 7, 8, 16, 33 };

        // Dynamic Way of Array Creation
        System.out.print("Enter the Size of an Array :- ");
        int n = sc.nextInt();
        int[] a = new int[n];

        for (int i = 0; i < a.length; i++) {
            System.out.print("Enter the Elements of an Array [ " + i + " ] :- ");
            a[i] = sc.nextInt();
        }

        int[] f_count = new int[a.length];

        // Creating a Count Array to Store the No. of Factors that a Number has
        // According to their Array order
        for (int i = 0; i < a.length; i++) {
            int count = 0;
            for (int j = a[i] - 1; j > 0; j--) {
                if (a[i] % j == 0) {
                    count++;
                }
            }
            f_count[i] = count;
        }

        // Printing the Given Array with the Help of Method
        System.out.println("Given Array : - ");
        printingArray(a);
        System.out.println();
        System.out.println("The Factors of the Given Array :-");
        printingArray(f_count);

        // Sorting the Count Array and Swapping the Given Array
        for (int i = 0; i < f_count.length - 1; i++) {
            for (int j = 0; j < f_count.length - 1; j++) {
                if (f_count[j] < f_count[j + 1]) {
                    // Swapping elements of the Count Array
                    int temp_c = f_count[j];
                    f_count[j] = f_count[j + 1];
                    f_count[j + 1] = temp_c;
                    // Swapping the Elements according to Count Array
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }

            }
        }

        // Printing the Resultant Array with the help of Method
        System.out.println("\n\n");
        System.out.println("Resultant Array : - ");
        printingArray(a);
        System.out.println();
        System.out.println("The Factors of the Resultant Array :-");
        printingArray(f_count);

    }

    // Printing the Array
    public static void printingArray(int[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
    }

}

我通过为数字所具有的因子创建一个新的数组来解决这个问题,并对因子进行排序,在对因子进行排序的过程中,我交换了给定数组的元素
有没有什么合理的方法可以简单地解决这些问题?
问题是:
将给定的数字按其因子数的降序排列。
输入:4,2,8,16,5
输出:16,8,4,5,2(或)16,8,4,2,5

nx7onnlm

nx7onnlm1#

如果阵列非常大,您是否担心性能?
你可以稍微改进一下冒泡排序。在外部for循环的第一次迭代之后,内部for循环会将因子数最多的元素移到数组的开头。所以,在下一次迭代中,该元素可以被跳过。所以,你应该可以将for (int j = 0; j < f_count.length - 1; j++) {改为for (int j = i; j < f_count.length - 1; j++) {。这将减少大约一半的比较次数。
但是,无论哪种方式,比较的次数都是O(n^2)。有更快的排序,例如合并排序,运行时间为O(n log n)。Java提供内置排序,在Arrays APICollections API中。
Arrays API中的sort方法被重载为只使用“自然顺序”对原语进行排序。但是,你不想对整数按“自然顺序”进行排序。你想创建一个Comparator,它允许你提供逻辑来进行比较。要做到这一点,你需要一个对象数组或Collection,而不是原语数组。
你可以将数据放入一个List<Integer>或一个Integer的数组中。这将允许使用CollectionsArrays中接受Comparator参数的排序方法。但是,这将导致每个元素的重复计算。潜在地,将有2 * n * lg n的因子计算。
既然你必须有一个对象来使用内置的排序,为什么不有一个class,其中每个示例都有原始值和因子的数量呢?

package decreasingintacctofactors; 

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class DecreasingIntAccToFactors {

    static class NumberFactorPair {

        int number;
        int factorCount;

        NumberFactorPair() {
        number = 0;
        factorCount = 0;
    }

    NumberFactorPair(int num) {
        number = num;
        factorCount = 2; // number is a factor of itself, 
                         // and 1 is a factor
        for (int i = 2; i <= num / 2; i++) {
            if (num % i == 0) {
                factorCount++;
            }
        }
    }
}


static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {
    // Static Way of Array Creation
    // int[] a = { 3, 4, 7, 8, 16, 33 };
    //        NumberFactorPair [] a = {
    //              new NumberFactorPair ( 3), new NumberFactorPair ( 4)
    //            , new NumberFactorPair ( 7), new NumberFactorPair ( 8)
    //            , new NumberFactorPair (16), new NumberFactorPair (33)
    //        };

    // Dynamic Way of Array Creation
    System.out.print("Enter the Size of an Array :- ");
    int n = sc.nextInt();
    NumberFactorPair [] a = new NumberFactorPair [n];

    for (int i = 0; i < a.length; i++) {
        System.out.print
           ("Enter the Elements of an Array [ " + i + " ] :- ");                     
        a[i] = new NumberFactorPair (sc.nextInt());            
    }    
    
    Arrays.sort (a, new Comparator<NumberFactorPair>() { 
        @Override
        public int compare (NumberFactorPair nf1, NumberFactorPair nf2) {
            return Integer.compare (nf2.factorCount, nf1.factorCount);
        }
    });    

    // Printing the Given Array with the Help of Method
    System.out.println("Given Array : - ");
    printingArray(a, false);
    System.out.println();
    System.out.println("The Factors of the Given Array :-");
    printingArray (a, true); 

    // Printing the Resultant Array with the help of Method
    System.out.println("\n\n");
    System.out.println("Resultant Array : - ");
    printingArray(a, false);
    System.out.println();
    System.out.println("The Factors of the Resultant Array :-");
    printingArray(a,true);
}

// Printing the Array
public static void printingArray(NumberFactorPair[] a, boolean factors) {
    for (int i = 0; i < a.length; i++) {
        System.out.print
          ((factors ? a[i].factorCount : a[i].number) + " ");
    }
  }
}

字段factorCount是从number派生而来的,但它的存在使得一个示例的因子数只计算一次。

相关问题