diff options
| author | Steven Wilson <steven1170@zoho.eu> | 2020-06-27 16:39:16 +0100 |
|---|---|---|
| committer | Steven Wilson <steven1170@zoho.eu> | 2020-06-27 16:39:16 +0100 |
| commit | ea242cfbc9597f63d533bdfc39c698f314ca2c01 (patch) | |
| tree | 59f387adc0160a916efcc9179bc052c15e248fa3 | |
| parent | 4d7fcf49b224801d35718a59e2b0f0089ab03aea (diff) | |
| download | perlweeklychallenge-club-ea242cfbc9597f63d533bdfc39c698f314ca2c01.tar.gz perlweeklychallenge-club-ea242cfbc9597f63d533bdfc39c698f314ca2c01.tar.bz2 perlweeklychallenge-club-ea242cfbc9597f63d533bdfc39c698f314ca2c01.zip | |
add rust solution for task 1
| -rw-r--r-- | challenge-066/steven-wilson/rust/ch-1.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/challenge-066/steven-wilson/rust/ch-1.rs b/challenge-066/steven-wilson/rust/ch-1.rs new file mode 100644 index 0000000000..2303f09f9d --- /dev/null +++ b/challenge-066/steven-wilson/rust/ch-1.rs @@ -0,0 +1,22 @@ +fn main() { + println!("5 / 2 = {}", divide( 5, 2 )); + println!("-5 / 2 = {}", divide( -5, 2 )); + println!("-5 / -2 = {}", divide( -5, -2 )); +} + +fn divide ( m: i32, n: i32) -> i32 { + let mut a = m.abs(); + let b = n.abs(); + let mut q = 0; + while a >= b { + q += 1; + a -= b; + } + if ( m < 0 && n > 0 ) || ( n < 0 && m > 0 ) { + q *= -1; + if a != 0 { + q -= 1; + } + } + q +} |
