Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to count the number of possible triangles from a given list in Rust

1 Answer

0 votes
// Triangle = the sum of any two values (sides) > than the third value (third side)
 
// A + B > C
// B + C > A
// C + A > B

#![allow(non_snake_case)]

fn CountTriangles(arr : [usize; 7]) -> usize {
    let size : usize = arr.len();
    let mut count : usize = 0;
    {
        let mut i : usize = 0;
            while i < size {
            {
                let mut j : usize = i + 1;
                while j < size {
                    {
                        let mut k : usize = j + 1;
                        while k < size {
                            if arr[i as usize] + arr[j as usize] > arr[k as usize] && 
                                arr[i as usize] + arr[k as usize] > arr[j as usize] && 
                                arr[k as usize] + arr[j as usize] > arr[i as usize] {
                                count += 1;
                                print!("{} + {} > {} | ", arr[i as usize], arr[j as usize], arr[k as usize]);
                                print!("{} + {} > {} | ", arr[i as usize], arr[k as usize], arr[j as usize]);
                                println!("{} + {} > {}", arr[k as usize], arr[j as usize], arr[i as usize]);
                            }
                            k += 1;
                        }
                    }
                    j += 1;
                }
            }
            i += 1;
        }
    }
    return count;
}

fn main() {
	let arr: [usize; 7] = [120, 80, 13, 16, 9, 14, 19];
    
    let total_triangles : usize = CountTriangles(arr);
    
    print!("Total triangles : {}", total_triangles);
}

 
 
 
 
 
/*
run:
 
13 + 16 > 9 | 13 + 9 > 16 | 9 + 16 > 13
13 + 16 > 14 | 13 + 14 > 16 | 14 + 16 > 13
13 + 16 > 19 | 13 + 19 > 16 | 19 + 16 > 13
13 + 9 > 14 | 13 + 14 > 9 | 14 + 9 > 13
13 + 9 > 19 | 13 + 19 > 9 | 19 + 9 > 13
13 + 14 > 19 | 13 + 19 > 14 | 19 + 14 > 13
16 + 9 > 14 | 16 + 14 > 9 | 14 + 9 > 16
16 + 9 > 19 | 16 + 19 > 9 | 19 + 9 > 16
16 + 14 > 19 | 16 + 19 > 14 | 19 + 14 > 16
9 + 14 > 19 | 9 + 19 > 14 | 19 + 14 > 9
Total triangles : 10
 
*/

 



answered Apr 15, 2023 by avibootz
...