公倍数与公约数
#include<stdio.h> #include <iostream> #include<math.h> using namespace std; //公约数 int getCommoDivisor(int x, int y) { int k; if (x < y) { k = x; x = y; y = k; } if (x%y == 0) return abs(y); else return getCommoDivisor(y, x%y); } //公倍数 int getCommonMultiple(int x, int y) { int z = getCommoDivisor(x, y); int t = (x*y) / z; return t; } int _tmain(int argc, _TCHAR* argv[]) { do { int x, y, z, t; cout << "请输入x=" << endl; cin >> x; cout << "请输入y=" << endl; cin >> y; z = getCommoDivisor(x, y); t = getCommonMultiple(x, y); cout << x << "与" << y << "的最小公倍数为:" << t << endl; cout << x << "与" << y << "的最大公约数为:" << z << endl; //getchar(); system("pause"); system("cls"); } while (1); return 0; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步