总共24个,答案肯定对的。
我是用HTML的图:
答案如下:
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
1 5 6
1 5 10
1 5 11
1 6 7
1 6 8
1 6 9
1 6 10
1 6 11
1 7 8
1 7 9
1 8 9
1 10 11
2 5 6
3 5 10
4 5 11
5 6 9
6 7 10
6 8 11
total have 24 triangles
逻辑更加简化:
a,b,c 构成三角形的条件为:
ab,bc,ac 均为线段,且abc不为线段。
用的是python:
#!/usr/bin/python # total 11 dots alldots=11 # total 7 lines allines=7 lines=[[1,2,6],[1,3,7,10],[1,4,8,11],[1,5,9],[2,3,4,5],[6,7,8,9],[5,6,10,11]] def isline(a,b): for i in range(0,allines): if( (a in lines[i]) and (b in lines[i])) : return 1 return 0 def isline3(a,b,c): for i in range(0,allines): if( (a in lines[i]) and (b in lines[i]) and (c in lines[i])) : return 1 return 0 def istriangle(a,b,c): if( isline(a,b) and isline(a,c) and isline(b,c) ): if(isline3(a,b,c) ) : return 0 else : return 1 return 0 count=0 # for each 3 dots check if they are a triangle. for i in range(1,alldots+1) : for j in range(i+1,alldots+1) : for k in range(j+1,alldots+1) : if ( istriangle(i,j,k) ) : print(i,j,k ) count = count+1 print ("total have ",count," triangles")
用JS换一种思路写个试试
输出:
1,2,3
1,2,4
1,2,5
1,3,4
1,3,5
1,4,5
1,5,6
1,5,10
1,5,11
1,6,7
1,6,8
1,6,9
1,6,10
1,6,11
1,7,8
1,7,9
1,8,9
1,10,11
2,5,6
3,5,10
4,5,11
5,6,9
6,7,10
6,8,11
一共 24个三角形
代码:
<html> <body> <script> var dotArr=new Array(); var inLineArr=new Array(); var dot = new Array([2,3,4,5,6,7,8,9,10,11],[1,3,4,5,6],[1,2,4,5,7,10],[1,2,3,5,8,11],[1,2,3,4,6,9,10,11],[1,2,5,7,8,9,10,11],[1,3,6,8,9,10],[1,4,6,7,9,11],[1,5,6,7,8],[1,3,5,6,7,11],[1,4,5,6,8,10]); var inLineArray= new Array([1,2,6],[1,3,7,10],[1,4,8,11],[1,5,9],[2,3,4,5],[6,7,8,9],[5,6,10,11]); for(i=0;i<dot.length;i++){ for (j =0; j<dot[i].length;j++){ if((i+1)<dot[i][j]){ for (k =0; k<dot[dot[i][j]-1].length;k++){ if(dot[i][j]<dot[dot[i][j]-1][k]&&(dot[i].indexOf(dot[dot[i][j]-1][k])!=-1)){ dotArr.push((i+1)+","+dot[i][j]+","+dot[dot[i][j]-1][k]); } } } } } for(var l=0;l<inLineArray.length;l++){ var i=0 if(inLineArray[l].length-i>2) { for (;i<inLineArray[l].length;i++){ var j=i+1 if(inLineArray[l].length-j>1){ for (;j<(inLineArray[l].length);j++){ var k=j+1 if(inLineArray[l].length-k>0){ for (;k<inLineArray[l].length;k++){ inLineArr.push(inLineArray[l][i]+","+inLineArray[l][j]+","+inLineArray[l][k]) } } } } } } } var k=0; for(var i=0;i<dotArr.length;i++){ var hasEle= false; foronce: { for(var j=0;j<inLineArr.length;j++){ if(dotArr[i]==inLineArr[j]){ hasEle = true; break foronce; } } if(!hasEle) document.write(dotArr[i]+"<br>"); k=k+1; } } document.write("一共 "+k+"个三角形<br>") </script> </body> </html>
大概意思:
1. 定义二维数组,标出每个点可以连接到的其他点
2. 遍历所有可能出现的三角形组合(包括三点共线的情况)
3. 遍历所有共线
4. 去除共线情况,输出数组
4. 我写的的确很烂