codewars--js--Two Joggers--求最小公倍数、最大公约数
问题描述:
Two Joggers
Description
Bob and Charles are meeting for their weekly jogging tour. They both start at the same spot called "Start" and they each run a different lap, which may (or may not) vary in length. Since they know each other for a long time already, they both run at the exact same speed.
Illustration
Example where Charles (dashed line) runs a shorter lap than Bob:

Task
Your job is to complete the function nbrOfLaps(x, y) that, given the length of the laps for Bob and Charles, finds the number of laps that each jogger has to complete before they meet each other again, at the same time, at the start.
The function takes two arguments:
- The length of Bob's lap (larger than 0)
- The length of Charles' lap (larger than 0)
The function should return an array (Tuple<int, int> in C#) containing exactly two numbers:
- The first number is the number of laps that Bob has to run
- The second number is the number of laps that Charles has to run
Examples:
nbrOfLaps(5, 3); // returns [3, 5]
nbrOfLaps(4, 6); // returns [3, 2]
解题思路: 很明显就是先求这两个数的最小公倍数,然后再分别除以Bob、Charles的长度,即可得他们俩分别的圈数。
lcm:最小公倍数,a*b/gcd
gcd:最大公约数(greatest common divisor,简写为gcd;可用辗转相除法得到
我的答案:
var nbrOfLaps = function (x, y) {
var a=gcd(x,y);
var b=y/a;
var c=x/a;
return [b, c];
}
function gcd(a,b){
var temp;
if(a<b){temp=a;a=b;b=temp;}
while(b!=0){
temp=a%b;
a=b;
b=temp;
}
return a;
}
优秀答案:
(1)
1 var nbrOfLaps = function(x, y) {
2 var lcm = x;
3 while(lcm % y != 0) {lcm += x;}
4 return [lcm / x, lcm / y];
5 }
(2)
function gcd(a, b) {
if(b == 0)
return a;
return gcd(b, a % b);
}
var nbrOfLaps = function (x, y) {
var lcm = (x*y)/ gcd(x,y);
return [lcm/x, lcm/y];
}
codewars--js--Two Joggers--求最小公倍数、最大公约数的更多相关文章
- 常见算法:C语言求最小公倍数和最大公约数三种算法
最小公倍数:数论中的一种概念,两个整数公有的倍数成为他们的公倍数,当中一个最小的公倍数是他们的最小公倍数,相同地,若干个整数公有的倍数中最小的正整数称为它们的最小公倍数,维基百科:定义点击打开链接 求 ...
- C语言求最小公倍数和最大公约数三种算法(经典)
把以前写的一些经验总结汇个总,方便给未来的学弟学妹们做个参考! --------------------------永远爱你们的:Sakura 最小公倍数:数论中的一种概念,两个整数公有的倍数成为他们 ...
- C语言求最小公倍数和最大公约数三种算法
最小公倍数:数论中的一种概念,两个整数公有的倍数成为他们的公倍数,其中一个最小的公倍数是他们的最小公倍数,同样地,若干个整数公有的倍数中最小的正整数称为它们的最小公倍数,维基百科:定义点击打开链接 求 ...
- 代码代码:输入两个正整数m和n,求其最大公约数和最小公倍数。15 20 5
import java.util.Scanner; //输入两个正整数m和n,求其最大公约数和最小公倍数.15 20 5 public class Test { public static void ...
- NEFU 116 两仪剑法 【求最小公倍数】
题目链接:http://acm.nefu.edu.cn/JudgeOnline/status.php?problem_id=116&order=1 解题思路:求最小公倍数 #include&l ...
- HDU - 1019-Least Common Multiple(求最小公倍数(gcd))
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...
- C语言中如何求最大公约数及如何求最小公倍数。
最大公约数: ...
- 输入两个正整数m和n,求其最大公约数和最小公倍数
public static void main(String[] args){ Scanner sc = new Scanner (System.in); int a,b; System.out ...
- c++求最小公倍数和最小公约数
方法一:辗转相除法(欧几里得 Euclidean) 用“较大数”除以“较小数”,再用较小数除以第一余数,再用第一余数除以第二余数: 反复直到余数为零为止. #include<iostream&g ...
- HDU 1019(求最小公倍数 **)
题意是求一组数的最小公倍数,不用存,每次输入即刻处理即可. 补充一点:两个自然数的最大公约数与它们的最小公倍数的乘积等于这两个数的乘积. 代码如下: #include <bits/stdc++. ...
随机推荐
- JS前端时间格式化
var dateTime = new Date(tree_time); tree_time = dateTime.getFullYear() + '-'+ (dateTime.getMonth()+1 ...
- 个人第四次作业AIpha2版本测试(最终版)
这个作业属于哪个课程 软件工程 作业要求在哪里 作业要求 团队名称 RainbowPlan团队博客 这个作业目标 手动测试非本团队的小组程序,是否可以正常登录,正常运行 一.测试人员信息 测试人员 姓 ...
- Spring Cache 抽象(缓存抽象) Redis 缓存
积少成多 ---- 仅以此致敬和我一样在慢慢前进的人儿 相关内容: https://blog.51cto.com/14230003/2369413?source=dra ...
- HDFS NameNode重启优化
http://tech.meituan.com/namenode-restart-optimization.html 一.背景 在Hadoop集群整个生命周期里,由于调整参数.Patch.升级等多种场 ...
- IDEA启动报错Plugin Error Problems found loading plugins的解决办法
错误描述 今天启动项目时发现IDEA控制台出错,tomcat的标志变成问号,启动不了服务器 Problems found loading plugins: Plugin "Persisten ...
- DataTable 相关
1.对表的初始化 //创建表 DataTable table = new DataTable(); //添加列 table.Columns.Add("ID", typeof(Int ...
- Vue中的计算属性
一.什么是计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护. 二.计算属性的用法 在一个计算属性里可以完成各种复杂的逻辑,包括运算.函 ...
- Java 架构知识点整理
架构学习 1. Java 核心技术 1.1. 基础知识 1.1.1. 进制转换 1.1.2. 异常处理 1.1.3. List 分批代码 1.1.4. 字符串分割 1.1.5. 编码风格 1.2. 并 ...
- win10下安装Anaconda3
1.官方下载:https://www.anaconda.com/distribution/#download-section (最新版直接下即可) 或者国内镜像下载:https://mirrors. ...
- ROS机器人话题之自定义消息
ROS提供了丰富的内建消息,std_msgs包定义了一些基本的类型. 具体例子 首先定义一个消息类型的文件叫做Complex 例Complex.msg float32 real float32 ima ...