A good big picture has the wood, haha, the topic comes, count the number of triangles in the above figure.
I really have been very serious to count, I follow the number of triangles composed of polygons, from 1-9 to several, but very sad, I put a polygon composed of nine of the triangle (sweat one, my grandmother all know only five), the results count 28. Finally, with the code to confirm the results, only 24 , carefully examined, only to find this error. The following is the code I write with C11, have to say C11 in a lot of new grammar, really very useful.
#include <iostream> #include <vector> #include <algorithm> #include <stdarg.h> using namespace std;typedef int vertex;typedef vector<vertex> line;//collinear n points for an edge typedef struct _GRAPH{VECTOR<VERTEX>_ Vertexes;vector<line>_lines;} GRAPH; GRAPH g_graph;int g_count = 0;bool in_same_line (VERTEX va, VERTEX vb) {Auto itor = G_graph._lines.begin (); while (itor! = G_g Raph._lines.end ()) {if (Find (Itor->begin (), Itor->end (), VA)! = Itor->end () &&find (Itor->begin (), Itor->end (), VB)! = Itor->end ()) {return true;} itor++;} return false;} BOOL In_same_line (VERTEX va, VERTEX vb, VERTEX vc) {Auto itor = G_graph._lines.begin (); while (itor! = G_graph._lines.end ()) {if (Find (Itor->begin (), Itor->end (), VA)! = Itor->end () &&find (Itor->begin (), Itor->end (), VB) ! = Itor->end () &&find (Itor->begin (), Itor->end (), vc)! = Itor->end ()) {return true;} itor++;} return false;} void Add_vertex (int index, ...) {va_list argptr; Va_start (argptr, index); Initializes the variable pointer int sum = index; index = Va_arg (argptr, int); As the parameter type for the next parameter type, returns an indeterminate argument while (1)//At the end of a specific character indicating the end {G_graph._vertexes.push_back (index); index = Va_arg (argptr, int); if (index>10) {break;}} Va_end (ARGPTR); }//add an edge void add_side (int vertex, ...) {va_list argptr; Va_start (argptr, vertex); Initializes the variable pointer int sum = vertex; Vertex = Va_arg (argptr, int); As the parameter type for the next parameter type, return the indeterminate parameter line line;//c Save the first indeterminate argument while (1)//At the end of a specific character indicating end {line.push_back (vertex); vertex = Va_arg (argptr, int); if (vertex>10) {break;}} Va_end (ARGPTR); G_graph._lines.push_back (line);} Initialize diagram void init () {//Add vertex Add_vertex (0,1,2,3,4,5,6,7,8,9,10);//Add Segment Add_side (0,1,7); Add_side (0,2,6,8); Add_side ( 0,3,5,9); Add_side (0,4,10); Add_side (1,2,3,4); Add_side (4,5,6,7); Add_side (7,8,9,10);} Computes the number of triangles void count () {Auto vertexes = g_graph._vertexes;for (size_t i=0; i<vertexes.size (); i++) {for (size_t j=i+1; j <vertexes.size (); J + +) {for (size_t k=j+1; k<vertexes.size (); k++) {if (In_same_line (I,J) &&in_same_line (i,k) &&in_same_line (j,k) &&!in_same_line (i,j,k)) {g_count++;}}}} void Main () {init (); count (); Cout<<g_count;system ("Pause");}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + number triangle