当前位置:网站首页>第二章:求a,b的最大公约与最小公倍数经典求解,求a,b的最大公约与最小公倍数常规求解,求n个正整数的的最大公约与最小公倍数
第二章:求a,b的最大公约与最小公倍数经典求解,求a,b的最大公约与最小公倍数常规求解,求n个正整数的的最大公约与最小公倍数
2022-07-03 19:16:00 【股_四】
//求a,b的最大公约与最小公倍数经典求解
int main()
{
long a, b, c, r, a1, b1;
printf(" 请输入正整数 a,b: ");
scanf("%ld,%ld", &a, &b);
if (a < b)
{
c = a;
a = b;
b = c;
}
a1 = a;
b1 = b;
r = a % b;
while (r != 0) //实施“辗转相除
{
a = b;
b = r;
r = a % b;
}
printf("(%ld,%ld) = %ld\n", a1, b1, b);
printf("{%ld,%ld} = %ld\n", a1, b1, a1 * b1 / b);
return 0;
}
结果:
//求a,b的最大公约与最小公倍数常规求解
int main()
{
long a, b, c;
printf(" 请输入正整数 a,b: ");
scanf("%ld,%ld", &a, &b);
if (a < b)
{
c = a;
a = b;
b = c;
}
for (c = b; c >= 1; c--)
if (a % c == 0 && b % c == 0) break; //C为试商因数
printf("(%ld,%ld) = %ld\n", a, b, c);
printf("{%ld,%ld} = %ld\n", a, b, a * b / c);
return 0;
}
结果:
//求n个正整数的的最大公约与最小公倍数
int main()
{
int k, n;
long a, b, c, m[100];
printf("请输入正整数个数n: ");
scanf("%d", &n);
printf("请依次输入%d个正整数:", n);
for (k = 0; k <= n - 1; k++)
{
printf("\n请输入第 %d个正整数:", k + 1);
scanf(" %ld", &m[k]); //输入原始数据
}
b = m[0];
for (k = 1; k <= n - 1; k++) // 循环计算 n - 1 次
{
a = m[k];
if (a < b)
{
c = a;
a = b; //交换a、b,确保a>b
b = c;
}
for (c = b; c >= 1; c--)
if (a % c == 0 && b % c == 0) break; //计算最大公约数
if (c == 1) break; // 如果求得最大公约数为 1,退出循环
b = c;
}
printf(" (%ld", m[0]); // 输出最大公约数结果
for (k = 1; k <= n - 1; k++)
printf(", %ld", m[k]);
printf(") = %ld\n", c);
b = m[0];
for (k = 1; k <= n - 1; k++) //循环计算 n-1 次
{
a = m[k];
if (a < b)
{
c = a;
a = b;
b = c;
}
for (c = a; c <= a * b; c = c + a)
if (c % b == 0) break;
b = c;
}
printf("{%ld", m[0]);
for (k = 1; k <= n - 1; k++)
printf(",%ld", m[k]);
printf("}=%ld\n", c);
return 0;
}
结果:
边栏推荐
- The installation path cannot be selected when installing MySQL 8.0.23
- How to design a high concurrency system
- Free year-end report summary template Welfare Collection
- SSM integration - joint debugging of front and rear protocols (list function, add function, add function status processing, modify function, delete function)
- 我们做了一个智能零售结算平台
- Redis master-slave synchronization, clustering, persistence
- Summary of learning materials and notes of Zhang Fei's actual combat electronics 1-31
- Max of PHP FPM_ Some misunderstandings of children
- [academic related] how to find the innovation of top papers? Chinese universities won the CVPR Best Student Thesis Award for the first time
- P1891 crazy LCM (Euler function)
猜你喜欢

Using the visualization results, click to appear the corresponding sentence

Record: solve the problem that MySQL is not an internal or external command environment variable

235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】

Integrated easy to pay secondary domain name distribution system

Add control at the top of compose lazycolumn

DriveSeg:动态驾驶场景分割数据集

2020 intermediate financial management (escort class)

Common text labels
![[mathematical modeling] ship three degree of freedom MMG model based on MATLAB [including Matlab source code 1925]](/img/a9/d89ee2b88517eea6b3c38d72cf099f.jpg)
[mathematical modeling] ship three degree of freedom MMG model based on MATLAB [including Matlab source code 1925]

Sentinel source code analysis part I sentinel overview
随机推荐
Dart JSON编码器和解码器剖析
Why should we do feature normalization / standardization?
Sustainable service business models
High concurrency Architecture - read write separation
论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
Flutter网络和数据存储框架搭建 -b1
【数学建模】基于matlab船舶三自由度MMG模型【含Matlab源码 1925期】
Think of new ways
Processing of user input parameters in shell script
Ego planner code parsing Bspline_ Optimizer section (3)
Pecan — Overview
利用可视化结果,点击出现对应的句子
Record: writing MySQL commands
[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD
The most valuable thing
【疾病识别】基于matlab GUI机器视觉肺癌检测系统【含Matlab源码 1922期】
Sentinel source code analysis part I sentinel overview
235. The nearest common ancestor of the binary search tree [LCA template + same search path]
Octopus online ecological chain tour Atocha protocol received near grant worth $50000
__ Weak and__ The difference between blocks