本帖最后由 bin554385863 于 2019-11-18 02:38 编辑 #include <stdio.h>
#include <limits.h>
typedef struct
{
int MAXCommonDivisor;
int MinCommonMultiple;
} num;
num func1(const int n, const int m)
{
num res = {-1, -1};
int tmax = m > n ? m : n;
int tmin = m < n ? m : n;
if (tmax >= __INT32_MAX__ )
{
return res;
}
else
{
for (size_t i = tmax;; i++)//最小公倍数一定大于等于较大的数
{
if (i % m == 0 && i % n == 0)
{
res.MinCommonMultiple = i;
break;
}
}
for (size_t i = tmin; i > 0; i--)//最小公约数一定小于等于较小的数,且1是所有数的公约数
{
if (m % i == 0 && n % i == 0)
{
res.MAXCommonDivisor = i;
break;
}
}
}
return res;
}
int main(int argc, char const *argv[])
{
int a = 24, b = 288;
int m = 37, n = 15;
printf("%d & %d--(%d , %d)\n", a, b, func1(a, b).MAXCommonDivisor, func1(a, b).MinCommonMultiple);
printf("%d & %d--(%d , %d)\n", m, n, func1(m, n).MAXCommonDivisor, func1(n, m).MinCommonMultiple);
return 0;
}
------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.18363.476]
(c) 2019 Microsoft Corporation。保留所有权利。
E:\Users\admin\Documents\VScode>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-uzatlvzi.lj2 --stdout=Microsoft-MIEngine-Out-rjc5cw52.bqv --stderr=Microsoft-MIEngine-Error-r3ancrla.4xx --pid=Microsoft-MIEngine-Pid-ic40r14d.fft --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
24 & 288--(24 , 288)
37 & 15--(1 , 555)
E:\Users\admin\Documents\VScode> |