热门标签 | 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

推荐阅读
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • Java 类成员初始化顺序与数组创建
    本文探讨了Java中类成员的初始化顺序、静态引入、可变参数以及finalize方法的应用。通过具体的代码示例,详细解释了这些概念及其在实际编程中的使用。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • 在金融和会计领域,准确无误地填写票据和结算凭证至关重要。这些文件不仅是支付结算和现金收付的重要依据,还直接关系到交易的安全性和准确性。本文介绍了一种使用C语言实现小写金额转换为大写金额的方法,确保数据的标准化和规范化。 ... [详细]
  • MQTT技术周报:硬件连接与协议解析
    本周开发笔记重点介绍了在新项目中使用MQTT协议进行硬件连接的技术细节,涵盖其特性、原理及实现步骤。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
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社区 版权所有