- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面给出的是一个求可变数量数字的最小公倍数的程序。
例如:如果我必须找到 3,9,13 的 lcm 那么它执行如下:长厘米(1,3)长厘米(3,9)lcm(9,13)
我只想知道这个程序的复杂性是多少。是 O(n) 还是 O(n^2)。你能告诉我为什么会这样吗?
#include <stdio.h>
int gcd(int x,int y)
{
int n;
if(x>y)
n=y;
else
n=x;
while(n>=0){
if(x%n==0 && y%n==0){
return n;
break;
}
n--;
}
return 1;
}
int lcm(int a,int b)
{
return a*b/gcd(a,b);
}
int main()
{
int tot,i,l=1;
int n[10];
printf("Enter the total numbers:");
scanf("%d",&tot);
if(tot>10 || tot<2){
printf("Sorry invalid inputs");
return 1;
}
printf("Enter the numbers one by one:");
for(i=0;i<tot;i++)
scanf("%d",&n[i]);
for(i=0;i<tot;i++){
l=lcm(l,n[i]);
}
printf("The LCM is %d",l);
return 0;
}
最佳答案
gcd
方法的复杂度(也是 lcm 方法的复杂度)为 O(n),其中 n 为 max(x, y)
。这是因为在最坏的情况下 x 和 y 互质,这意味着 n 必须递减到 1。Euclid 的 GCD 算法更快:http://en.wikipedia.org/wiki/Euclidean_algorithm
关于c - 我的 LCM 程序的复杂性是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23577867/
我正在尝试将游戏中的 GPS map 系统复制到网络中。基本上游戏中有 12 张 map ,每张 map 都有不同的安全区域,你可以在其中扭曲你的 Angular 色或走动,但我一路上遇到了一些问题。
给定一个根目录,逐行读取 rootDirectory 或子目录中的所有文件,并对每个文件中的所有数字求和。每个文件的每一行都有一个编号。所以我只需要读取所有文件并对所有数字求和并返回。我想出了下面的代
我使用的是 MySQL 5.5,有两个表 T1(ID, NAME) 和 T2(ID, MARKS),下面是表中的数据。 T1的数据 ID NAME 1 A 2 B 3 C T2的数据 ID MA
我必须确定以下函数的时间复杂度(大 O): void BET::makeEmpty(BinaryNode* &n) { if(n != NULL) { makeEmpt
我基本上了解如何计算函数的复杂度。这同样适用于确定数学函数的增长顺序。 [我可能不像我想的那样理解它,这就是为什么我可能会问这个。] 例如: an^3 + bn^2 + cn + d 可以用大 O 表
我有一个家庭作业问题,一段时间以来我一直在努力解决,但我终其一生都无法解决。 我有一张尺寸为 X*Y 的纸和一组尺寸较小的图案,以及与之相关的价格值。我可以水平或垂直切割板材,我必须找到优化的切割模式
我写了这个字符串所有排列的解决方案。我对这个解决方案的时间和空间复杂性有疑问。我假设时间复杂度为 O(n³),因为嵌套循环和递归以及空间复杂度为 O(n),因为递归。 我的假设是否正确?如果可以,有没
刚遇到这个问题: 子集求和问题:求给定数组中两对数字的总和等于给定数字的个数 例如:给定总和为 9,数组为 { 0, 1, 2, 7, 13 } => O/P 为 1 对(2 和 7) 似乎这可以在
鉴于我有一个包含一组单词的文件: 1) 如果我选择一个哈希表来存储单词 -> 计数,查找特定单词出现的时间复杂度是多少? 2) 我怎样才能按字母顺序返回这些单词? 如果我选择哈希表,我知道 1) 的时
我很难理解大 O 时间复杂度。 Big O 的正式定义: f(n) = O(g(n)) means there are positive constants c and k, such that 0
有人知道 ECMAScript5 的 Object.keys() 在常见实现中的时间复杂度吗? n 个键的时间复杂度是 O(n) 吗?假设采用哈希实现,时间与哈希表的大小成正比吗? 我正在寻找语言实现
我需要知道,就复杂性而言,什么更好。要么为每个按钮从 xml 中标识一个单独的 onClick 方法,如下所示: android:onClick:"clickHandler" 和java代码: pub
无论如何我都不是 Javascript 的新手;也就是说,我对 bind 的作用及其行为方式有了基本的了解。但是,我遇到了一个我对结果有点不确定的用例。让我详细说明一个示例设置: 示例设置 首先,我有
这个问题在这里已经有了答案: Python list.clear() time and space complexity? (4 个答案) 关闭 1 年前。 Python 3 方法 list.cle
我创建的这个计算两个数组的交集是线性的方法的复杂度(在良好、平均、最差的情况下)? O(n) public void getInt(int[] a,int[] b){ int i=0; int
假设我已经使用 PriorityQueue 实现了 dijkstras,因此在未访问的节点中添加和删除需要 O(log n)。 PQ 最多包含 E 个节点,因此清空它我们得到 O(E)。当 PQ 不为
我能得到一些帮助来理解如何解决这个教程问题吗?我仍然不明白教授的解释。我不确定如何计算第三个/最内层循环的大 0。她解释说,该算法的答案是 O(n^2),并且第二个和第三个循环必须被视为具有 O(n)
有人可以告诉我这个过程在 for 迭代中的时间复杂度吗?这段代码是FloydWarshall算法的“重构路径”部分。prev[n][n]是最短路径中源节点和目的节点之间的节点矩阵。printAllSP
运行时间、复杂性、编译时间和执行时间有什么区别? 运行时间与时间复杂度有冲突,执行时间和执行时间有什么区别? 最佳答案 您真正需要的是如何将大O时间复杂度转换为运行时。这不像一开始看起来那么容易。 因
我对算法复杂度的计算很困惑。对于一项任务,我们被赋予以下功能并要求找到它的复杂性。 int selectkth(int a[], int k, int n) { int i, j, mini,
我是一名优秀的程序员,十分优秀!