问题描述:

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:

  1. The length of Bob's lap (larger than 0)
  2. The length of Charles' lap (larger than 0)

The function should return an array (Tuple<int, int> in C#) containing exactly two numbers:

  1. The first number is the number of laps that Bob has to run
  2. 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--求最小公倍数、最大公约数的更多相关文章

  1. 常见算法:C语言求最小公倍数和最大公约数三种算法

    最小公倍数:数论中的一种概念,两个整数公有的倍数成为他们的公倍数,当中一个最小的公倍数是他们的最小公倍数,相同地,若干个整数公有的倍数中最小的正整数称为它们的最小公倍数,维基百科:定义点击打开链接 求 ...

  2. C语言求最小公倍数和最大公约数三种算法(经典)

    把以前写的一些经验总结汇个总,方便给未来的学弟学妹们做个参考! --------------------------永远爱你们的:Sakura 最小公倍数:数论中的一种概念,两个整数公有的倍数成为他们 ...

  3. C语言求最小公倍数和最大公约数三种算法

    最小公倍数:数论中的一种概念,两个整数公有的倍数成为他们的公倍数,其中一个最小的公倍数是他们的最小公倍数,同样地,若干个整数公有的倍数中最小的正整数称为它们的最小公倍数,维基百科:定义点击打开链接 求 ...

  4. 代码代码:输入两个正整数m和n,求其最大公约数和最小公倍数。15 20 5

    import java.util.Scanner; //输入两个正整数m和n,求其最大公约数和最小公倍数.15 20 5 public class Test { public static void ...

  5. NEFU 116 两仪剑法 【求最小公倍数】

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/status.php?problem_id=116&order=1 解题思路:求最小公倍数 #include&l ...

  6. HDU - 1019-Least Common Multiple(求最小公倍数(gcd))

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  7. C语言中如何求最大公约数及如何求最小公倍数。

    最大公约数:                                                                                               ...

  8. 输入两个正整数m和n,求其最大公约数和最小公倍数

    public static void main(String[] args){  Scanner sc = new Scanner (System.in);  int a,b;  System.out ...

  9. c++求最小公倍数和最小公约数

    方法一:辗转相除法(欧几里得 Euclidean) 用“较大数”除以“较小数”,再用较小数除以第一余数,再用第一余数除以第二余数: 反复直到余数为零为止. #include<iostream&g ...

  10. HDU 1019(求最小公倍数 **)

    题意是求一组数的最小公倍数,不用存,每次输入即刻处理即可. 补充一点:两个自然数的最大公约数与它们的最小公倍数的乘积等于这两个数的乘积. 代码如下: #include <bits/stdc++. ...

随机推荐

  1. java面试| 精选基础题(3)

    每天进步一点点,距离大腿又近一步! 阅读本文大概需要6分钟 系列文章 java面试| 精选基础题(1) java面试|精选基础题(2) 1.float f=3.4;是否正确? 答:不正确,编译无法通过 ...

  2. Nito.AsyncEx 这个库

    有一个非常聪明的小伙子 (我高度赞扬) 叫 Stephen Cleary ,他写了一个很棒的 Extension 集,共同参与开发的还有 Stephen Toub (他显然是经验丰富的),所以我充分信 ...

  3. 用自定义变量作为动作方法参数 URL路由 精通ASP-NET-MVC-5-弗瑞曼

  4. 「雅礼集训 2017 Day2」棋盘游戏

    祝各位圣诞后快乐(逃) 题目传送门 分析: 首先棋盘上的路径构成的图是一张二分图 那么对于一个二分图,先求出最大匹配,先手如果走到关键匹配点,只要后手顺着匹配边走,由于不再会出现增广路径,所以走到最后 ...

  5. Ubuntu16手动安装OpenStack

    记录大佬的博客全文转载于https://www.voidking.com/dev-ubuntu16-manual-openstack-env/ 前言 <Ubuntu16安装OpenStack&g ...

  6. 源码详解系列(七) ------ 全面讲解logback的使用和源码

    什么是logback logback 用于日志记录,可以将日志输出到控制台.文件.数据库和邮件等,相比其它所有的日志系统,logback 更快并且更小,包含了许多独特并且有用的特性. logback ...

  7. ios--->ios消息机制(NSNotification 和 NSNotificationCenter)

    问题的背景 IOS中委托模式和消息机制基本上开发中用到的比较多,一般最开始页面传值通过委托实现的比较多,类之间的传值用到的比较多,不过委托相对来说只能是一对一,比如说页面A跳转到页面B,页面的B的值改 ...

  8. CORS解决跨域问题的几种方法

    一 后端服务器使用过滤器 新建过滤器: /** * 解决跨域 */ public class AccessControlAllowOriginFilter implements Filter { @O ...

  9. 保存网页内容到excel

    from selenium import webdriverfrom time import sleepfrom selenium.common.exceptions import NoSuchEle ...

  10. 手把手写框架入门(一) | 核心拦截器DispatchFilter实现

    前言 1Filter实现框架拦截 1配置自定义Filter 2创建一个Filter 3创建一个ActionMapping 4创建一个ActionMapper 5创建一个WebExecutor 6创建测 ...