標籤:

下圖有多少個三角形?

下圖有多少個三角形


數了半天,受不鳥了,還是用程序吧.

參考微博里的27個三角形是怎麼數出來的? - 葉飛影的回答中的程序代碼.

struct Segment
{
char start; // 線段起點
char end; // 線段終點
char point1; // 線段經過的點
char point2; // 線段經過的點
};

void main()
{
const int segments_count = 35;

Segment segments[segments_count] =
{
{"A", "B", 0, 0},
{"A", "C", "G", "H"},
{"A", "D", "F", "J"},
{"A", "E", 0, 0},
{"A", "F", 0, 0},
{"A", "G", 0, 0},
{"A", "H", "G", 0},
{"A", "J", "F", 0}, //8

{"B", "C", 0, 0},
{"B", "D", "H", "J"},
{"B", "E", "F", "G"},
{"B", "F", "G", 0},
{"B", "G", 0, 0},
{"B", "H", 0, 0},
{"B", "I", "H", 0}, // 7

{"C", "D", 0, 0},
{"C", "E", "I", "J"},
{"C", "G", "H", 0},
{"C", "H", 0, 0},
{"C", "I", 0, 0},
{"C", "J", "I", 0}, //6

{"D", "E", 0, 0},
{"D", "F", "J", 0},
{"D", "H", "I", 0},
{"D", "I", 0, 0},
{"D", "J", 0, 0}, // 5

{"E", "F", 0, 0},
{"E", "G", "F", 0},
{"E", "I", "J", 0},
{"E", "J", 0, 0}, // 4

{"F", "G", 0, 0},
{"F", "J", 0, 0},
{"G", "H", 0, 0},
{"H", "I", 0, 0},
{"I", "J", 0, 0}, // 5
};

int triangles_count = 0;
char szTriange[4] = {0};

for (int s1 = 0; s1 &< segments_count; s1++) { for (int s2 = s1 + 1; s2 &< segments_count; s2++) { if (segments[s1].end != segments[s2].start ) { continue; } if (segments[s1].point1 == segments[s2].end || segments[s1].point2 == segments[s2].end || segments[s2].point1 == segments[s1].start || segments[s2].point2 == segments[s1].start ) { // 三點共線 continue; } for (int s3 = 0; s3 &< segments_count; s3++) { if (segments[s1].start == segments[s3].start segments[s2].end == segments[s3].end) { if (segments[s3].point1 != segments[s1].end segments[s3].point2 != segments[s1].end) { ++triangles_count; szTriange[0] = segments[s1].start; szTriange[1] = segments[s1].end; szTriange[2] = segments[s2].end; printf("%s ", szTriange); } } } } } printf("%d ", triangles_count); }

運行結果如下:

36個嗎?


用JS換一種思路寫個試試

輸出:

1,2,3

1,2,4

1,2,5

1,2,6

1,2,7

1,2,8

1,3,4

1,3,5

1,4,5

1,5,6

1,5,7

1,5,9

1,6,7

1,6,9

1,7,8

2,3,6

2,3,8

2,4,7

2,5,6

2,5,7

2,5,10

2,6,7

2,6,8

2,6,10

3,5,6

4,5,7

4,5,9

5,6,7

5,7,9

5,7,10

6,7,8

6,7,9

6,7,10

6,8,10

7,9,10

一共 35個三角形代碼:

&
&

&

var dotArr=new Array();
var inLineArr=new Array();

var dot = new Array();
dot[0] = [2,3,4,5,6,7,8,9]
dot[1] = [1,3,4,5,6,7,8,10]
dot[2] = [1,2,4,5,6,8]
dot[3] = [1,2,3,5,7,9]
dot[4] = [1,2,3,4,6,7,9,10]
dot[5] = [1,2,3,5,7,8,9,10]
dot[6] = [1,2,4,5,6,8,9,10]
dot[7] = [1,2,3,6,7,10]
dot[8] = [1,4,5,6,7,10]
dot[9] = [2,5,6,7,8,9]

var inLineArray= new Array([1,3,6,8],[1,4,7,9],[2,3,4,5],[2,7,8,10],[5,6,9,10]);

for(i=0;i&2)
{
for (;i&1){
for (;j&<(inLineArray[l].length);j++){ var k=j+1 if(inLineArray[l].length-k&>0){
for (;k&");
k=k+1;
}

}

document.write("一共 "+k+"個三角形&")

&
&

&

大概意思:

1. 定義二維數組,標出每個點可以連接到的其他點

2. 遍歷所有可能出現的三角形組合(包括三點共線的情況)

3. 遍歷所有共線,去除共線情況,輸出

4. 我寫的仍然很爛


正確答案是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):
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")


這張圖中能數出多少個三角形?


根據前面的 @謝丹 的思路,用racket重寫了一遍。答案的確是35。

代碼如下:

#lang racket
(require rackunit)
(require racket/local)
;; data difinitions
; a Dot is a N represent a dot on the line.
;
; a Line is one of:
; (list Dot Dot)
; (append (list Dot Dot) [List-of Dot])
; the first two Dot represent the endpoints;
; the other Dot represent the line"s crossing points.

; a Graph is a [List-of Line]
; all lines of the Graph is not the same.

;; constants
(define alldots-1 10) ; total 10 dots
(define lines-1
"((1 2)
(2 3)
(3 4)
(4 5)
(5 1)
(1 3 7 8)
(3 5 9 10)
(5 2 6 7)
(2 4 8 9)
(4 1 10 6)))

;; functions
; Number Graph -&> N
; produces the number of triangles in the given g.
(define (count-triangles dot-num g)
(local ((define max-num (add1 dot-num))
; [List-of Number] -&> Number
; produces the sum of the given lon
(define (sum lon)
(foldr + 0 lon))
; N -&> [List-of N]
; produces a default list of number which starts from
; the given s and end at max-num, the step is 1.
(define (default-range s)
(range s max-num 1))
; [List-of Dot] -&> Boolean
; determine whether points are all on the same line of g.
(define (is-line? points)
(local (; [List-of Dots] -&> [Line -&> Boolean]
; produces a function which determine whether lop is on the
; line
(define (on-line lop)
(lambda (line) (andmap (lambda (p) (member p line)) lop)))
(define satisfied-lines
(filter (on-line points) g))
; Graph -&> Boolean
; determine whether l has a line.
(define (exist-line? l)
(not (empty? l))))
(exist-line? satisfied-lines)))
; [List Dot Dot Dot]-&> Boolean
; determine whether the given lo3p could consist a triangle on g.
(define (is-triangle? lo3p)
(let* ([3p (list-&>vector lo3p)]
[a (vector-ref 3p 0)]
[b (vector-ref 3p 1)]
[c (vector-ref 3p 2)])
(and (andmap (lambda (lop) (is-line? lop))
`((,a ,b) (,b ,c) (,c ,a)))
(not (is-line? lo3p)))))
)
(sum (for*/list ((a (default-range 1))
(b (default-range (add1 a)))
(c (default-range (add1 b))))
(if (is-triangle? `(,a ,b ,c)) 1 0)))))

;; test
(check-equal? (count-triangles alldots-1 lines-1) 35
"not the correct number of triangles.")


先從由一個三角形開始看 有10個 再看由2個三角形構成的有10個 由3個三角形構成的有10個 由四個構成的沒有 由5個構成的有5個 所以一共有35個


謝邀。居然有人邀請我回答這種問題。。

數了兩遍,我覺得是35個吧。。就分一下類然後數一數。


推薦閱讀:

為什麼彭羅斯(不可能)三角形可以在紙上畫出來?也是利用了視覺錯誤嗎?
如何用尺規作圖的方式作頂點分別在三平行線上的正三角形?
三角形的三條中線相等,能證明這是等邊三角形嗎(或角平分線、高線)?

TAG:三角形 |