《求符合要求的自然数对》

当前编程题:考试题---《求符合要求的自然数对》

15.

【问题描述】

编程输出符合如下要求的自然数对:它们的和为667,它们的最小公倍数除以最大公约数的商为120。输出格式为每对一行,小数在前,大数在后,两数间用逗号分隔,多对则按每对中小数的大小升序排列先后。

【输入形式】

无输入。


【输出形式】


每个自然数对一行,小数在前,大数在后,两数间用逗号分隔,多对则按每对中小数的大小升序排列先后。

关于详细的求 最大公约数与最小公倍数-

https://blog.csdn.net/qq_41682681/article/details/80787349

下面附上我的代码:

#include <iostream>
#include <cmath>
using namespace std;


bool factor(int a,int b)
{
    int temp,dive=0;
    int l = a * b;
    int q=a,w=b;
    if(a < b)//保证a始终大于b
    {
        temp = a;
        a = b;
        b = temp;
    }
    while(b)
    {
        dive = b;
        b = a % b;
        a = dive;
    }
    int c = l / dive;
    if(c/dive==120)
    {
        cout << q << "," << w<<endl;
    }
}
int main()
{
    for(int i = 1;i< 667/2;i++)
    {
        factor(i,667-i);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41682681/article/details/80787745