当前位置:网站首页>第二章:求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;
}
结果:
边栏推荐
- leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]
- EGO Planner代码解析bspline_optimizer部分(1)
- QT -- qfile file read / write operation
- Driveseg: dynamic driving scene segmentation data set
- Day18 - basis of interface testing
- HOW TO WRITE A DAILY LAB NOTE?
- The most valuable thing
- Compared with 4G, what are the advantages of 5g to meet the technical requirements of industry 4.0
- leetcode:556. Next larger element III [simulation + change as little as possible]
- Summary of learning materials and notes of Zhang Fei's actual combat electronics 1-31
猜你喜欢
OSPF - detailed explanation of stub area and full stub area
[mathematical modeling] ship three degree of freedom MMG model based on MATLAB [including Matlab source code 1925]
【学术相关】顶级论文创新点怎么找?中国高校首次获CVPR最佳学生论文奖有感...
我眼中真正优秀的CTO长啥样
What is the content of game modeling
[leetcode] [SQL] notes
【疾病识别】基于matlab GUI机器视觉肺癌检测系统【含Matlab源码 1922期】
Add control at the top of compose lazycolumn
SQL injection for Web Security (1)
Counting from the East and counting from the West will stimulate 100 billion industries. Only storage manufacturers who dare to bite the "hard bone" will have more opportunities
随机推荐
记录在模拟器中运行flutter时报的错
Why should the gradient be manually cleared before back propagation in pytorch?
Free sharing | linefriends hand account inner page | horizontal grid | not for sale
Understanding of database architecture
UE source code analysis: uccharactermovementcomponent - rootmotion
Streaming media server (16) -- figure out the difference between live broadcast and on-demand
[disease identification] machine vision lung cancer detection system based on Matlab GUI [including Matlab source code 1922]
High concurrency Architecture - distributed search engine (ES)
The online customer service system developed by PHP is fully open source without encryption, and supports wechat customer service docking
The earliest record
Bad mentality leads to different results
math_泰勒公式
High concurrency Architecture - read write separation
Leetcode: 11. Récipient contenant le plus d'eau [double pointeur + cupidité + enlèvement de la plaque la plus courte]
Pecan — Overview
235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】
Ctrip will implement a 3+2 work system in March, with 3 days on duty and 2 days at home every week
Scrape crawler framework
【光学】基于matlab涡旋光产生【含Matlab源码 1927期】
Day_ 18 IO stream system