输入2个正整数m和n,求其最大公约数和最小公倍数,用c语言写程序
2个回答
展开全部
输入两个正整数m和n, 求其最大公约数和最小公倍数. <1> 用辗转相除法求最大公约数 算法描述: m对n求余为a, 若a不等于0 则 m <- n, n <- a, 继续求余 否则 n 为最大公约数 <2> 最小公倍数 = 两个数的积 / 最大公约数
#include int main()
{
int m, n; int m_cup, n_cup, res; /*被除数, 除数, 余数*/
printf("Enter two integer:\n");
scanf("%d %d", &m, &n);
if (m > 0 && n >0)
{
m_cup = m;
n_cup = n;
res = m_cup % n_cup;
while (res != 0)
{
m_cup = n_cup;
n_cup = res;
res = m_cup % n_cup;
}
printf("Greatest common divisor: %d\n", n_cup);
printf("Lease common multiple : %d\n", m * n / n_cup);
}
else printf("Error!\n");
return 0;
}
#include int main()
{
int m, n; int m_cup, n_cup, res; /*被除数, 除数, 余数*/
printf("Enter two integer:\n");
scanf("%d %d", &m, &n);
if (m > 0 && n >0)
{
m_cup = m;
n_cup = n;
res = m_cup % n_cup;
while (res != 0)
{
m_cup = n_cup;
n_cup = res;
res = m_cup % n_cup;
}
printf("Greatest common divisor: %d\n", n_cup);
printf("Lease common multiple : %d\n", m * n / n_cup);
}
else printf("Error!\n");
return 0;
}
展开全部
#include <stdio.h>
int gcd(int m, int n)
{
if(m%n) return gcd(n, m%n);
return n;
}
int lcm(int m, int n)
{
return m*n/gcd(m, n);
}
int main()
{
int m, n;
scanf("%d %d", &m, &n);
printf("gcd: %d, lcm: %d\n", gcd(m, n), lcm(m, n));
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询