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

整数范围内最频繁的因子

整数范围内最频繁的因子原文:https://www.gees

整数范围内最频繁的因子

原文:https://www . geesforgeks . org/最频繁整数范围因子/

给你一个数字范围,从较低的数字到较高的数字(包括两者)。考虑这些数字中除 1 以外的所有因素,任务是找出出现次数最多的因素。如果一个以上的号码有最大频率比打印他们中的任何一个。
例:

Input : lower=10 higher=14
Output : The factor with maximum frequency is 2.
Input : lower=11 higher=11
Output : The factor with maximum frequency is 11.

解释:
如果我们有一个数字范围,比如说 10 到 14。那么 1 以外的因素将是:


  • 10 => 2, 5, 10


  • 11 => 11


  • 12 => 2, 3, 4, 6, 12


  • 13 => 13


  • 14 => 2, 7, 14


在上述情况下,频率(2)= 3,与其他因素相比,这是最大值。
进场:
1。如果较低=较高,则所有因子的频率都为 1。所以我们可以打印任何因子的数字,因为每个数字都是自身的因子,所以我们打印相同的数字。
2。否则,我们打印“2”,因为它对于一系列数字总是正确的。

C++

// CPP program to find most common factor
// in a range.
#include
using namespace std;
int mostCommon(int lower, int higher)
{
    // Check whether lower number
    // and higher number are same
    if (lower == higher)
        return lower;
    else
        return 2;
}
// Driver Code
int main()
{
    int lower = 10; // Lower number
    int higher = 20; // Higher number
    printf("The most frequent factor %d\n",
               mostCommon(lower, higher));
    return 0;
}

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

// Java program to find most common factor
// in a range.
public class GfG {
    public static int mostCommon(int lower, int higher)
    {
        // Check whether lower number 
        // and higher number are same
        if (lower == higher) 
            return lower;
        else
            return 2;
    }
    // Driver code
    public static void main(String []args) {
        int lower = 10; // Lower number
        int higher = 20; // Higher number
        System.out.println("The most frequent factor " +
                                    mostCommon(lower, higher));
    }
}
// This code is contributed by Rituraj Jain

Python 3

# Python 3 program to find most
# common factor in a range.
def mostCommon( lower, higher):
    # Check whether lower number
    # and higher number are same
    if (lower == higher):
        return lower
    else:
        return 2
# Driver Code
lower = 10 # Lower number
higher = 20 # Higher number
print("The most frequent factor",
       mostCommon(lower, higher))
# This code is contributed by ash264

C

// C# program to find most common factor
// in a range.
using System;
class GFG
{
static int mostCommon(int lower, int higher)
{
    // Check whether lower number
    // and higher number are same
    if (lower == higher)
        return lower;
    else
        return 2;
}
// Driver Code
public static void Main()
{
    int lower = 10; // Lower number
    int higher = 20; // Higher number
    Console.WriteLine("The most frequent factor " +
                       mostCommon(lower, higher));
}
}
// This code is contributed
// by Akanksha Rai

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

// PHP program to find most common
// factor in a range.
function mostCommon($lower, $higher)
{
    // Check whether lower number
    // and higher number are same
    if ($lower == $higher)
        return $lower;
    else
        return 2;
}
// Driver Code
$lower = 10; // Lower number
$higher = 20; // Higher number
echo "The most frequent factor ".
    mostCommon($lower, $higher) . "\n";
// This code is contributed
// by Akanksha Rai
?>

java 描述语言


Output: 

The most frequent factor 2

时间复杂度: O(1)


推荐阅读
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 电话号码的字母组合解题思路和代码示例
    本文介绍了力扣题目《电话号码的字母组合》的解题思路和代码示例。通过使用哈希表和递归求解的方法,可以将给定的电话号码转换为对应的字母组合。详细的解题思路和代码示例可以帮助读者更好地理解和实现该题目。 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • 本文介绍了[从头学数学]中第101节关于比例的相关问题的研究和修炼过程。主要内容包括[机器小伟]和[工程师阿伟]一起研究比例的相关问题,并给出了一个求比例的函数scale的实现。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 怎么在PHP项目中实现一个HTTP断点续传功能发布时间:2021-01-1916:26:06来源:亿速云阅读:96作者:Le ... [详细]
  • 本文介绍了使用cacti监控mssql 2005运行资源情况的操作步骤,包括安装必要的工具和驱动,测试mssql的连接,配置监控脚本等。通过php连接mssql来获取SQL 2005性能计算器的值,实现对mssql的监控。详细的操作步骤和代码请参考附件。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • 本文由编程笔记#小编整理,主要介绍了关于数论相关的知识,包括数论的算法和百度百科的链接。文章还介绍了欧几里得算法、辗转相除法、gcd、lcm和扩展欧几里得算法的使用方法。此外,文章还提到了数论在求解不定方程、模线性方程和乘法逆元方面的应用。摘要长度:184字。 ... [详细]
author-avatar
你不懂_de_笑
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有