From 6d86ceef0ff40f8c6acd04df564ec5990e7b0e0e Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Sun, 5 Nov 2023 09:01:44 +0000 Subject: [PATCH] 028 ok --- 028/main.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 028/main.rs diff --git a/028/main.rs b/028/main.rs new file mode 100644 index 0000000..24d9f8f --- /dev/null +++ b/028/main.rs @@ -0,0 +1,55 @@ +fn get_matrix(n: usize) -> u32 { + let mut xss: Vec> = Vec::new(); + for _i in 0..n { + let mut r = Vec::new(); + for _j in 0..n { + r.push(0u32); + } + xss.push(r); + } + + xss[n/2][n/2] = 1; + + let mvs = [(1, 0), (0, -1), (-1, 0), (0, 1)]; + let mut mvi = 0usize; + let mut l = 1; + let mut li = 0; + let mut i: usize = n / 2; + let mut j: usize = n / 2 + 1; + let mut res = 1; + for k in 2u32..= (n * n) as u32 { + xss[i][j] = k; + let mv = mvs[mvi]; + //println!("mvi: {}", mvi); + if mv.0 == 1 { i += 1; } + if mv.0 == -1 { i -= 1; } + if mv.1 == 1 { j += 1; } + if mv.1 == -1 { j-= 1; } + li += 1; + //println!("li vs l: {} vs {}", li, l); + if li == l { + mvi += 1; + li = 0; + if mvi == 1 { l += 1; } + if mvi == 3 { l += 1; } + if mvi == 4 { mvi = 0; } + let d = if mvi == 0 { k } else { k + 1 }; + //println!("{}", d); + res += d; + } + } + + //for i in 0..n { + // for j in 0..n { + // print!("{}\t", xss[i][j]); + // } + // println!(""); + //} + + res +} + +fn main() { + println!("{}", get_matrix(1001)); +} +