You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
1.9 KiB

11 months ago
use std::collections::HashSet;
type Str = Vec<u8>;
type Res = HashSet<u32>;
fn pr(s: &Str) {
for c in s.iter() { print!("{}", c); }
println!("");
}
fn get_eq(s: &Str, p: usize, q: usize) -> (u32, u32, u32) {
let mut a: u32 = 0;
for i in 0..p {
a *= 10;
a += s[i] as u32;
}
let mut b: u32 = 0;
for i in p..q {
b *= 10;
b += s[i] as u32;
}
let mut c: u32 = 0;
for i in q..s.len() {
c *= 10;
c += s[i] as u32;
}
return (a, b, c);
}
fn test(s: &Str, hs: &mut Res) {
for p in 1..s.len() {
for q in p..s.len() {
let (a, b, c) = get_eq(&s, p, q);
//println!("{} * {} = {}", a, b, c);
if a * b == c {
hs.insert(c);
println!("{} * {} = {}", a, b, c);
}
}
}
}
fn sp(s: &Str, fx: usize, k: usize) -> Str {
let mut res = Vec::new();
for i in 0..fx {
res.push(s[i]);
}
res.push(s[k]);
for i in fx..s.len() {
if i != k { res.push(s[i]); }
}
res
}
fn lp(s: &Str, fx: usize, keep_first: bool, mut count: usize, hs: &mut Res) -> usize {
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 {
count += 1;
test(&ss, hs);
}
count = lp(&ss, fx + 1, false, count, hs);
}
count
}
fn main() {
let mut s = Vec::new();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(6);
s.push(7);
s.push(8);
s.push(9);
//test(&s);
let mut hs = HashSet::new();
lp(&s, 0, true, 0, &mut hs);
println!("count: {}", hs.len());
let mut sum = 0u32;
for p in hs {
sum += p;
}
println!("sum = {}", sum);
}