正确答案是35:
#!/usr/bin/python # total 10 dots alldots=10 # total 10 lines allines=10 # 定义所有线段 lines=[[1,2],[2,3],[3,4],[4,5],[1,5],[1,3,7,8],[2,4,8,9],[2,5,6,7],[3,5,9,10],[1,4,6,10]] def isline(a,b): #判断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): #判断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): # abc 三点,如果ab,bc,ac都是线段,且abc不在一条直线,就是三角形 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")