Count the number of groups of similar triangles in the Triangle Array

Source: Internet
Author: User

Struct triangle <br/>{< br/> triangle (INT _ A = 0, int _ B = 0, int _ c = 0) {A = _; B = _ B; C = _ c ;}< br/> int A, B, C; <br/> }; </P> <p> inline void swap (Int & LHS, Int & RHs) <br/>{< br/> int temp; <br/> temp = LHS; LHS = RHS; RHS = temp; <br/>}</P> <p> // <br/> // sort the side length of a triangle in ascending order. <br/> //////// //////////////////////////////////////// ///// // <br/> void sortedge (Triangle & TRI) <br/> {<br/> If (TRI. a> tri. b) {swap (TRI. a, Tri. b) ;}< br/> If (TRI. b> tri. c) {swap (TRI. b, Tri. c) ;}< br/> If (TRI. a> tri. b) {swap (TRI. a, Tri. B );} <br/>}</P> <p> // <br/> // determine whether the two triangles are similar. <br/> /////////// //////////////////////////////////////// //////////////////// <br/> bool issimilar (const triangle & tri1, const triangle & tri2) <br/>{< br/> int larger, smaller; <br/> larger = tri1.a; smaller = tri2.a; <br/> If (larger <smaller) Swap (larger, smaller); <br/> If (larger % smaller! = 0) return false; <br/> larger = tri1. B; smaller = tri2. B; <br/> If (larger <smaller) Swap (larger, smaller ); <br/> If (larger % smaller! = 0) return false; <br/> larger = tri1.c; smaller = tri2.c; <br/> If (larger <smaller) Swap (larger, smaller ); <br/> If (larger % smaller! = 0) return false; <br/> return true; <br/>}</P> <p> // <br/> // count the number of similar triangles <br/> /////////// //////////////////////////////////////// //// // <br/> int similarcount (Triangle * triarr, int size) <br/>{< br/> int COUNT = 0; <br/> int * flagarr = new int [size]; <br/> memset (flagarr, 0, sizeof (INT) * size); <br/> for (INT I = 0; I <size; I ++) <br/> {<br/> for (Int J = I + 1; j <size; j ++) <br/>{< br/> sortedge (triarr [I]); <br/> sortedge (triarr [J]); <br/> If (issimilar (triarr [I], triarr [J]) & flagarr [I] = 0 & flagarr [J] = 0) <br/>{< br/> count ++; <br/> flagarr [I] = flagarr [J] = 1; <br/>}< br/> Delete [] flagarr; <br/> return count; <br/>}</P> <p> int main (INT argc, char * argv []) <br/>{< br/> triangle triarray [10] = <br/>{< br/> triangle (2, 2, 3 ), <br/> triangle (4, 4, 6), <br/> triangle (9, 6, 6), <br/> triangle (3, 4, 2 ), <br/> triangle (3, 4, 2), <br/> triangle (2, 5, 6), <br/> triangle (4, 10, 12 ), <br/> triangle (1, 1, 1), <br/> triangle (2, 2, 2), <br/> triangle (3, 3, 3) <br/>}; <br/> cout <similarcount (triarray, 10); <br/> return 0; <br/>}< br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.