Last active
February 3, 2018 21:14
-
-
Save AnthonyMikh/d83f44790a75042446cad35d6851e996 to your computer and use it in GitHub Desktop.
Решение задачи №67 от UniLecs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type BaseCoord = i32; | |
type DeltaCoord = u32; | |
type InputPoint = (BaseCoord, BaseCoord); | |
//gcd - greatest common divisor | |
//Считает НОД аргументов по алгоритму Евклида | |
//В случае, если один из аргументов равен нулю, | |
//возвращает второй | |
fn gcd(a: DeltaCoord, b: DeltaCoord) -> DeltaCoord { | |
if a < b { gcd(b, a) } | |
else { | |
if b == 0 { a } | |
else { gcd(b, a % b) } | |
} | |
} | |
fn integer_points((x1, y1): InputPoint, (x2, y2): InputPoint) -> DeltaCoord { | |
let delta_x = (x2 - x1).abs() as DeltaCoord; //длина проекции отрезка на ось абсцисс | |
let delta_y = (y2 - y1).abs() as DeltaCoord; //длина проекции отрезка на ось ординат | |
gcd(delta_x, delta_y) + 1 | |
} | |
#[test] | |
fn do_tests() { | |
assert_eq!(integer_points((0, 5), (5, 0)), 6); | |
assert_eq!(integer_points((0, 0), (0, 6)), 7); | |
assert_eq!(integer_points((345, 1), (-345, -1)), 3); | |
assert_eq!(integer_points((0, 0), (341, 235)), 2); | |
assert_eq!(integer_points((2, 8), (10, 0)), 9); | |
assert_eq!(integer_points((-3, 5), (6, 17)), 4); | |
assert_eq!(integer_points((-10_000, 10_000), (10_000, -10_000)), 20_001); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ссылка на playground: https://play.rust-lang.org/?gist=9be95997143639fa8c249374b1cf64c5&version=stable