📌  相关文章
📜  按其乘法持久性的递增顺序对数组进行排序

📅  最后修改于: 2021-09-03 13:55:50             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是通过将每个数组元素的数字递归相乘来获得一位数所需的步数,以递增的顺序对数组进行排序。如果任意两个数字的步数相同,则先打印较小的数字。

例子

方法:给定的问题可以通过找到获得一位数所需的步数来解决,方法是将每个数组元素的数字递归相乘,然后使用比较器函数按递增顺序对数组进行排序。请按照以下步骤解决问题:

  • 声明一个比较器函数cmp(X, Y) ,它将两个元素作为参数并执行以下步骤:
    • 迭代一个循环,直到X变成一位数,并将X的值更新为其数字的乘积。
    • 对值Y重复上述步骤。
    • 如果X的值小于Y ,则返回true 。否则,返回false
  • 使用上述比较器函数sort(arr, arr + N, cmp)对给定数组arr[]进行排序
  • 完成上述步骤后,打印数组arr[]

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the number of
// steps required to reduce a given
// number to a single-digit number
int countReduction(int num)
{
    // Stores the required result
    int ans = 0;
 
    // Iterate until a single digit
    // number is not obtained
    while (num >= 10) {
 
        // Store the number in a
        // temporary variable
        int temp = num;
        num = 1;
 
        // Iterate over all digits and
        // store their product in num
        while (temp > 0) {
            int digit = temp % 10;
            temp = temp / 10;
            num *= digit;
        }
 
        // Increment the answer
        // by 1
        ans++;
    }
 
    // Return the result
    return ans;
}
 
// Comparator function to sort the array
bool compare(int x, int y)
{
    // Count number of steps required
    // to reduce X and Y into single
    // digits integer
    int x1 = countReduction(x);
    int y1 = countReduction(y);
 
    // Return true
    if (x1 < y1)
        return true;
 
    return false;
}
 
// Function to sort the array according
// to the number of steps required to
// reduce a given number into a single
// digit number
void sortArray(int a[], int n)
{
    // Sort the array using the
    // comparator function
    sort(a, a + n, compare);
 
    // Print the array after sorting
    for (int i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 39, 999, 4, 9876 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    sortArray(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
class GFG
{
 
// Function to find the number of
// steps required to reduce a given
// number to a single-digit number
static int countReduction(int num)
{
   
    // Stores the required result
    int ans = 0;
 
    // Iterate until a single digit
    // number is not obtained
    while (num >= 10)
    {
 
        // Store the number in a
        // temporary variable
        int temp = num;
        num = 1;
 
        // Iterate over all digits and
        // store their product in num
        while (temp > 0) {
            int digit = temp % 10;
            temp = temp / 10;
            num *= digit;
        }
 
        // Increment the answer
        // by 1
        ans++;
    }
 
    // Return the result
    return ans;
}
 
// Function to sort the array according
// to the number of steps required to
// reduce a given number into a single
// digit number
static void sortArray(Integer a[], int n)
{
   
    // Sort the array using the
    // comparator function
    Arrays.sort(a,new Comparator(){
        public int compare(Integer x, Integer y)
   {
           
            // Count number of steps required
    // to reduce X and Y into single
    // digits integer
    int x1 = countReduction(x);
    int y1 = countReduction(y);
 
    // Return true
    if (x1 < y1)
        return -1;
 
    return 1;
        }
    });
 
    // Print the array after sorting
    for (int i = 0; i < n; i++)
    {
        System.out.print(a[i] + " ");
    }
}
 
  // Driver code
public static void main (String[] args)
{
     Integer arr[] = { 39, 999, 4, 9876 };
    int N = arr.length;
 
    // Function Call
    sortArray(arr, N);
}
}
 
// This code is contributed by offbeat


输出:
4 9876 39 999

时间复杂度: O(N * log(N) * log(X)),其中X是数组中最大的元素,arr[]
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live