主题:有道题我不会做,有谁会做?!
有一个数字三角形如下:从顶层走到底层,每次往下走,只能选向左或向右两个方向走,找出数字之和为30的所有路径。
7
/ \
4 6
/ \/ \
6 9 3
/ \ / \/ \
6 3 7 1
/ \ / \ / \/ \
2 5 3 2 8
/ \ / \ / \ / \/ \
5 9 4 7 3 2
我已打了一个程序,不知是否还有更简便的:
var a:array[1..6,1..6]of integer;
c,d,f,b,e,s,i,j:integer;
begin
for i:=1 to 6 do
for j:=1 to i do
read(a[i,j]);
for b:=1 to 2 do
for c:=1 to 3 do
for d:=1 to 4 do
for e:=1 to 5 do
for f:=1 to 6 do begin
s:=7+a[2,b]+a[3,c]+a[4,d]+a[5,e]+a[6,f];
if (s=30)and((b-1=1)or(b-1=0))and((c-b=0)or(c-b=1))
and((d-c=0)or(d-c=1))and((e-d=0)or(e-d=1))and
((f-e=0)or(f-e=1))
then
writeln('7',' ',a[2,b],' ',a[3,c],' ',a[4,d],' ',
a[5,e],' ',a[6,f]);
end;
end.[em70][em70]
7
/ \
4 6
/ \/ \
6 9 3
/ \ / \/ \
6 3 7 1
/ \ / \ / \/ \
2 5 3 2 8
/ \ / \ / \ / \/ \
5 9 4 7 3 2
我已打了一个程序,不知是否还有更简便的:
var a:array[1..6,1..6]of integer;
c,d,f,b,e,s,i,j:integer;
begin
for i:=1 to 6 do
for j:=1 to i do
read(a[i,j]);
for b:=1 to 2 do
for c:=1 to 3 do
for d:=1 to 4 do
for e:=1 to 5 do
for f:=1 to 6 do begin
s:=7+a[2,b]+a[3,c]+a[4,d]+a[5,e]+a[6,f];
if (s=30)and((b-1=1)or(b-1=0))and((c-b=0)or(c-b=1))
and((d-c=0)or(d-c=1))and((e-d=0)or(e-d=1))and
((f-e=0)or(f-e=1))
then
writeln('7',' ',a[2,b],' ',a[3,c],' ',a[4,d],' ',
a[5,e],' ',a[6,f]);
end;
end.[em70][em70]