Browse Source

041 ok

master
Coin de Gamma 5 months ago
parent
commit
2657e336c2
  1. 89
      041/main.rs

89
041/main.rs

@ -0,0 +1,89 @@
use std::collections::HashSet;
use std::collections::VecDeque;
type Str = VecDeque<u8>;
type Res = HashSet<u32>;
fn pr(s: &Str) {
for c in s.iter() { print!("{}", c); }
println!("");
}
fn is_prime(k: u32) -> bool {
if k < 0 {
return false;
}
for i in 2..k {
if k % i == 0 {
return false;
}
}
return true;
}
fn nstr(n: u32) -> Str {
let mut res = Str::new();
let mut nn = n;
while nn > 0 {
res.push_front((nn % 10) as u8);
nn /= 10;
}
res
}
fn strn(s: &Str) -> u32 {
let mut res = 0;
for d in s.iter() {
res *= 10;
res += *d as u32;
}
res
}
fn test(s: &Str, hs: &mut Res) {
let n = strn(s);
if is_prime(n) {
//println!("{}", n);
hs.insert(n);
}
}
fn sp(s: &Str, fx: usize, k: usize) -> Str {
let mut res = Str::new();
for i in 0..fx {
res.push_back(s[i]);
}
res.push_back(s[k]);
for i in fx..s.len() {
if i != k { res.push_back(s[i]); }
}
res
}
fn lp(s: &Str, fx: usize, keep_first: bool, hs: &mut Res) {
let kf = 0; //if keep_first { 0usize } else { 1usize };
for k in (fx + kf)..s.len() {
let ss = sp(&s, fx, k);
//count += 1;
if ss != *s || keep_first {
test(&ss, hs);
}
lp(&ss, fx + 1, false, hs);
}
}
fn main() {
let mut hs = HashSet::new();
let mut s = nstr(1234567); // manual test of length
let mut max = 0;
let m = lp(&s, 0, true, &mut hs);
for h in hs.iter() {
if *h > max {
max = *h;
}
}
println!("{}", max);
}
Loading…
Cancel
Save