📜  产生一个数字,使得每个数字的频率是数字乘以给定数字的频率

📅  最后修改于: 2021-05-31 20:51:49             🧑  作者: Mango

给定数字N 仅包含从1到9的数字。任务是使用数字N生成一个新数字,以使新数字中每个数字的频率等于N中该数字的频率乘以数字本身。
注意:新数字中的数字必须按升序排列。
例子

想法是使用计数数组或哈希将数字的计数或频率存储在给定的数字N中。现在,对于每个数字,将其添加到新数字中,即K次,其中K等于计数数组中其频率乘以数字本身。
下面是上述方法的实现:

C++
// CPP program to print a number such that the
//  frequency of each digit in the new number is
// is equal to its frequency in the given number
// multiplied by the digit itself.
 
#include 
using namespace std;
 
// Function to print such a number
void printNumber(int n)
{
    // initializing a hash array
    int count[10] = { 0 };
 
    // counting frequency of the digits
    while (n) {
        count[n % 10]++;
        n /= 10;
    }
 
    // printing the new number
    for (int i = 1; i < 10; i++) {
        for (int j = 0; j < count[i] * i; j++)
            cout << i;
    }
}
 
// Driver code
int main()
{
    int n = 3225;
 
    printNumber(n);
 
    return 0;
}


Java
// Java program to print a number such that the
// frequency of each digit in the new number is
// is equal to its frequency in the given number
// multiplied by the digit itself.
 
import java.io.*;
 
class GFG {
  
// Function to print such a number
static void printNumber(int n)
{
    // initializing a hash array
    int count[] = new int[10];
 
    // counting frequency of the digits
    while (n>0) {
        count[n % 10]++;
        n /= 10;
    }
 
    // printing the new number
    for (int i = 1; i < 10; i++) {
        for (int j = 0; j < count[i] * i; j++)
            System.out.print(i);
    }
}
 
// Driver code
 
    public static void main (String[] args) {
        int n = 3225;
 
    printNumber(n);
    }
}
// This code is contributed by inder_verma


Python3
# Python 3 program to print a number such that the
# frequency of each digit in the new number is
# is equal to its frequency in the given number
# multiplied by the digit itself.
 
# Function to print such a number
def printNumber(n):
 
    # initializing a hash array
    count = [0]*10
 
    # counting frequency of the digits
    while (n) :
        count[n % 10] += 1
        n //= 10
 
    # printing the new number
    for i in range(1,10) :
        for j in range(count[i] * i):
            print(i,end="")
 
# Driver code
if __name__ == "__main__":
    n = 3225
 
    printNumber(n)
     
# This code is contributed by
# ChitraNayal


C#
// C# program to print a number such
// that the frequency of each digit
// in the new number is equal to its
// frequency in the given number
// multiplied by the digit itself.
using System;
 
class GFG
{
 
// Function to print such a number
static void printNumber(int n)
{
    // initializing a hash array
    int []count = new int[10];
 
    // counting frequency of
    // the digits
    while (n > 0)
    {
        count[n % 10]++;
        n /= 10;
    }
 
    // printing the new number
    for (int i = 1; i < 10; i++)
    {
        for (int j = 0;
                 j < count[i] * i; j++)
            Console.Write(i);
    }
}
 
// Driver code
public static void Main ()
{
    int n = 3225;
 
    printNumber(n);
}
}
 
// This code is contributed
// by inder_verma


PHP


Javascript


输出:
222233355555

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。