fn main() { println!("{}", Solution::judge_point24(vec![1, 5, 9, 1])); } struct Solution; impl Solution { pub fn judge_point24(cards: Vec) -> bool { let mut stack: Vec> = vec![cards.iter().map(|&v| v as f64).collect()]; while let Some(cards) = stack.pop() { println!("{:?}", cards); if cards.len() == 1 { if (cards.first().unwrap() - 24f64).abs() < 1e-6 { return true; } else { continue; } } for i in 0..(cards.len() - 1) { for j in (i + 1)..cards.len() { let a = cards[i]; let b = cards[j]; let mut left_cards = cards.clone(); left_cards.remove(j); left_cards.remove(i); println!("{i} {j} {:?}", left_cards); let mut new_cards = left_cards.clone(); new_cards.push(a + b); stack.push(new_cards); let mut new_cards = left_cards.clone(); new_cards.push(a * b); stack.push(new_cards); let mut new_cards = left_cards.clone(); new_cards.push(a - b); stack.push(new_cards); let mut new_cards = left_cards.clone(); new_cards.push(b - a); stack.push(new_cards); if b.abs() > 1e-6 { let mut new_cards = left_cards.clone(); new_cards.push(a / b); stack.push(new_cards); } if a.abs() > 1e-6 { let mut new_cards = left_cards.clone(); new_cards.push(b / a); stack.push(new_cards); } } } } false } }