LoginSignup
1
0

More than 5 years have passed since last update.

【codility】Lesson 3: Time Complexity FrogJmp

Posted at

subject

X:現在地, Y:目的地, D:ジャンプ幅としたときに、XからYまで同距離もしくはそれ以上の距離になるのは、ジャンプを何回したときか。
例)
X=10, Y = 85, D = 30と与えられた場合は、3を返す。
X + D*3 => 85となるためである。

submit

一発score100!とても気持ち良い。
L03_frogJmp.png

program

point:割り算の商と余りが出せる「div」を使ってみた
余りを出すには「%」を使えばよいけれど、今回は商も重要だったので良い方法がないか探してみたら、の中にあるdivが使えることが分かった。とっても便利。

frogJmp.cpp
#include<iostream>
#include <stdlib.h>
using namespace std;

int solution(int X, int Y, int D) {
    div_t times = div(Y-X, D);
    if (times.rem != 0)
        return (times.quot + 1);
    return times.quot;
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0