博客
关于我
16.最小公倍数
阅读量:133 次
发布时间:2019-02-27

本文共 576 字,大约阅读时间需要 1 分钟。

题目描述

正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。

输入描述:

输入两个正整数A和B。

输出描述:

输出A和B的最小公倍数。

示例1

输入

5 7

输出

35

 

 

解题思路

方法1

暴力求解

#include
using namespace std;int main(){ int A, B; while (cin >> A >> B) { int m = max(A, B); while (1) { if (m%A == 0 && m%B == 0) { cout << m << endl; break; } m++; } } return 0;}

 

 

 

方法2

最小公倍数=两数乘积/最大公约数

最大公约数-辗转相除法

#include
using namespace std;int gcd(int a, int b){ int c; while (c = a%b) { a = b; b = c; } return b;}int main(){ int a, b; while (cin >> a >> b) { cout << a*b / gcd(a, b) << endl; } return 0;}

 

转载地址:http://tpbb.baihongyu.com/

你可能感兴趣的文章
Netty工作笔记0021---NIO编写,快速入门---编写服务器
查看>>
Netty工作笔记0022---NIO快速入门--编写客户端
查看>>
Vue踩坑笔记 - 关于vue静态资源引入的问题
查看>>
Netty工作笔记0024---SelectionKey API
查看>>
Netty工作笔记0025---SocketChannel API
查看>>
Netty工作笔记0026---NIO 网络编程应用--群聊系统1---编写服务器1
查看>>
Netty工作笔记0027---NIO 网络编程应用--群聊系统2--服务器编写2
查看>>
Netty工作笔记0028---NIO 网络编程应用--群聊系统3--客户端编写1
查看>>
Netty工作笔记0029---NIO 网络编程应用--群聊系统4--客户端编写2
查看>>
Netty工作笔记0030---NIO与零拷贝原理剖析
查看>>
Netty工作笔记0031---NIO零拷贝应用案例
查看>>
Netty工作笔记0032---零拷贝AIO内容梳理
查看>>
Netty工作笔记0033---Netty概述
查看>>
Netty工作笔记0034---Netty架构设计--线程模型
查看>>
Netty工作笔记0035---Reactor模式图剖析
查看>>
Netty工作笔记0036---单Reactor单线程模式
查看>>
Netty工作笔记0037---主从Reactor多线程
查看>>
Netty工作笔记0038---Netty模型--通俗版
查看>>
Netty工作笔记0039---Netty模型--详细版
查看>>
Netty工作笔记0040---Netty入门--服务端1
查看>>