热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

按照频率降序打印数字

按照频率降序打印数字原文:https://www . geesforgeks . org/print-numbers-in-降序

按照频率降序打印数字

原文:https://www . geesforgeks . org/print-numbers-in-降序-连同它们的频率/

给定一个数组 arr ,任务是按照降序打印数组的元素及其频率。
例:

输入: arr[] = {1,3,3,3,4,4,5}
输出: 5 发生 1 次
4 发生 2 次
3 发生 3 次
1 发生 1 次
输入: arr[] = {1,1,1,2,3,4,9,10}
输出: 10 发生 1 次
9 发生 2 次【T15

简单方法:使用一些数据结构(例如多集),以降序存储元素,然后用它的计数逐个打印元素,然后从数据结构中删除它。对于所使用的数据结构,时间复杂度为 0(N 对数 N),辅助空间为 0(N)。
以下是上述方法的实现:

卡片打印处理机(Card Print Processor 的缩写)

// C++ program to print the elements in
// descending along with their frequencies
#include
using namespace std;
// Function to print the elements in descending
// along with their frequencies
void printElements(int a[], int n)
{
    // A multiset to store elements in decreasing order
    multiset > ms;
    // Insert elements in the multiset
    for (int i = 0; i         ms.insert(a[i]);
    }
    // Print the elements along with their frequencies
    while (!ms.empty()) {
        // Find the maximum element
        int maxel = *ms.begin();
        // Number of times it occurs
        int times = ms.count(maxel);
        cout <        // Erase the maxel
        ms.erase(maxel);
    }
}
// Driver Code
int main()
{
    int a[] = { 1, 1, 1, 2, 3, 4, 9, 9, 10 };
    int n = sizeof(a) / sizeof(a[0]);
    printElements(a, n);
    return 0;
}

Output: 

10 occurs 1 times
9 occurs 2 times
4 occurs 1 times
3 occurs 1 times
2 occurs 1 times
1 occurs 3 times

高效方法: 按降序排列数组,然后开始打印元素及其频率。
以下是上述方法的实施:

C++

// C++ program to print the elements in
// descending along with their frequencies
#include
using namespace std;
// Function to print the elements in descending
// along with their frequencies
void printElements(int a[], int n)
{
    // Sorts the element in decreasing order
    sort(a, a + n, greater());
    int cnt = 1;
    // traverse the array elements
    for (int i = 0; i         // Prints the number and count
        if (a[i] != a[i + 1]) {
            cout <            cnt = 1;
        }
        else
            cnt += 1;
    }
    // Prints the last step
    cout <}
// Driver Code
int main()
{
    int a[] = { 1, 1, 1, 2, 3, 4, 9, 9, 10 };
    int n = sizeof(a) / sizeof(a[0]);
    printElements(a, n);
    return 0;
}

Java 语言(一种计算机语言,尤用于创建网站)

// Java program to print the elements in
// descending along with their frequencies
import java.util.*;
class GFG
{
// Function to print the elements in descending
// along with their frequencies
static void printElements(int a[], int n)
{
    // Sorts the element in decreasing order
    Arrays.sort(a);
    a = reverse(a);
    int cnt = 1;
    // traverse the array elements
    for (int i = 0; i     {
        // Prints the number and count
        if (a[i] != a[i + 1])
        {
            System.out.print(a[i]+ " occurs " +
                            cnt + " times\n");
            cnt = 1;
        }
        else
            cnt += 1;
    }
    // Prints the last step
    System.out.print(a[n - 1]+ " occurs " +
                    cnt + " times\n");
}
static int[] reverse(int a[])
{
    int i, n = a.length, t;
    for (i = 0; i     {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
    return a;
}
// Driver Code
public static void main(String[] args)
{
    int a[] = { 1, 1, 1, 2, 3, 4, 9, 9, 10 };
    int n = a.length;
    printElements(a, n);
}
}
// This code is contributed by PrinciRaj1992

Python 3

# Python3 program to print the elements in
# descending along with their frequencies
# Function to print the elements in
# descending along with their frequencies
def printElements(a, n) :
    # Sorts the element in decreasing order
    a.sort(reverse = True)
    cnt = 1
    # traverse the array elements
    for i in range(n - 1) :
        # Prints the number and count
        if (a[i] != a[i + 1]) :
            print(a[i], " occurs ", cnt, "times")
            cnt = 1
        else :
            cnt += 1
    # Prints the last step
    print(a[n - 1], "occurs", cnt, "times")
# Driver Code
if __name__ == "__main__" :
    a = [ 1, 1, 1, 2,
          3, 4, 9, 9, 10 ]
    n = len(a)
    printElements(a, n)
# This code is contributed by Ryuga

C

// C# program to print the elements in
// descending along with their frequencies
using System;
class GFG
{
// Function to print the elements in descending
// along with their frequencies
static void printElements(int []a, int n)
{
    // Sorts the element in decreasing order
    Array.Sort(a);
    a = reverse(a);
    int cnt = 1;
    // traverse the array elements
    for (int i = 0; i     {
        // Prints the number and count
        if (a[i] != a[i + 1])
        {
            Console.Write(a[i]+ " occurs " +
                            cnt + " times\n");
            cnt = 1;
        }
        else
            cnt += 1;
    }
    // Prints the last step
    Console.Write(a[n - 1]+ " occurs " +
                    cnt + " times\n");
}
static int[] reverse(int []a)
{
    int i, n = a.Length, t;
    for (i = 0; i     {
        t = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = t;
    }
    return a;
}
// Driver Code
public static void Main(String[] args)
{
    int []a = { 1, 1, 1, 2, 3, 4, 9, 9, 10 };
    int n = a.Length;
    printElements(a, n);
}
}
// This code is contributed by PrinciRaj1992

服务器端编程语言(Professional Hypertext Preprocessor 的缩写)

// PHP program to print the elements in
// descending along with their frequencies
// Function to print the elements in
// descending along with their frequencies
function printElements(&$a, $n)
{
    // Sorts the element in
    // decreasing order
    rsort($a);
    $cnt = 1;
    // traverse the array elements
    for ($i = 0; $i <$n - 1; $i++)
    {
        // Prints the number and count
        if ($a[$i] != $a[$i + 1])
        {
            echo ($a[$i]);
            echo (" occurs " );
            echo $cnt ;
            echo (" times\n");
            $cnt = 1;
        }
        else
            $cnt += 1;
    }
    // Prints the last step
    echo ($a[$n - 1]);
    echo (" occurs ");
    echo $cnt;
    echo (" times\n");
}
// Driver Code
$a = array(1, 1, 1, 2, 3,
           4, 9, 9, 10 );
$n = sizeof($a);
printElements($a, $n);
// This code is contributed
// by Shivi_Aggarwal
?>

java 描述语言


Output: 

10 occurs 1 times
9 occurs 2 times
4 occurs 1 times
3 occurs 1 times
2 occurs 1 times
1 occurs 3 times

推荐阅读
author-avatar
驰诗勋
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有