当前位置:网站首页>第二章:求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;
}
结果:
边栏推荐
- [water quality prediction] water quality prediction based on MATLAB Fuzzy Neural Network [including Matlab source code 1923]
- Summary of composition materials for 2020 high-frequency examination center of educational resources
- Streaming media server (16) -- figure out the difference between live broadcast and on-demand
- Merge K ascending linked lists
- leetcode:11. Container with the most water [double pointer + greed + remove the shortest board]
- [disease identification] machine vision lung cancer detection system based on Matlab GUI [including Matlab source code 1922]
- Bad mentality leads to different results
- Common text labels
- “google is not defined” when using Google Maps V3 in Firefox remotely
- If the warehouse management communication is not in place, what problems will occur?
猜你喜欢

Foundation of ActiveMQ

Record: writing MySQL commands

Why should we do feature normalization / standardization?

SQL injection for Web Security (1)

What is the content of game modeling

QT -- qfile file read / write operation

Integrated easy to pay secondary domain name distribution system

2020 intermediate financial management (escort class)

The online customer service system developed by PHP is fully open source without encryption, and supports wechat customer service docking

利用可视化结果,点击出现对应的句子
随机推荐
Comments on flowable source code (37) asynchronous job processor
Processing of user input parameters in shell script
Max of PHP FPM_ Some misunderstandings of children
[leetcode weekly race] game 300 - 6110 Number of incremental paths in the grid graph - difficult
[disease identification] machine vision lung cancer detection system based on Matlab GUI [including Matlab source code 1922]
This Chinese numpy quick look-up table is too easy!
Hard disk monitoring and analysis tool: smartctl
High concurrency Architecture - distributed search engine (ES)
leetcode:556. Next larger element III [simulation + change as little as possible]
What does a really excellent CTO look like in my eyes
Latex image rotates with title
Using the visualization results, click to appear the corresponding sentence
Thesis study - 7 Very Deep Convolutional Networks for Large-Scale Image Recognition (3/3)
Smart wax therapy machine based on STM32 and smart cloud
How does if ($variable) work? [repeat] - how exactly does if ($variable) work? [duplicate]
Next spread
Today I am filled with emotion
Free sharing | linefriends hand account inner page | horizontal grid | not for sale
Why should the gradient be manually cleared before back propagation in pytorch?
Valentine's Day - make an exclusive digital collection for your lover