
pascal题NIOP普及。只要给个思路和算法 115
题目描述将1,2,••••••,9共9个数排成下列形态的三角形。abcdefghi其中:a~i分别...
题目描述
将1,2,••••••,9共9个数排成下列形态的三角形。
a
b c
d e
f g h i
其中:a~i分别表示1,2,••••••,9中的一个数字,并要求同时满足下列条件: (1)a < f < i; (2)b < d, g < h, c < e (3)a+b+d+f=f+g+h+i=i+e+c+a=P 程序要求: 根据输入的边长之和P 输出所有满足上述条件的三角形的个数。
输入
一行,一个整数P。
输出
一行,一个整数,即满足条件的三角形的个数。
样例输入
21
样例输出
4
提示
来源 展开
将1,2,••••••,9共9个数排成下列形态的三角形。
a
b c
d e
f g h i
其中:a~i分别表示1,2,••••••,9中的一个数字,并要求同时满足下列条件: (1)a < f < i; (2)b < d, g < h, c < e (3)a+b+d+f=f+g+h+i=i+e+c+a=P 程序要求: 根据输入的边长之和P 输出所有满足上述条件的三角形的个数。
输入
一行,一个整数P。
输出
一行,一个整数,即满足条件的三角形的个数。
样例输入
21
样例输出
4
提示
来源 展开
3个回答
展开全部
O(9!),暴搜即可
var n,r:longint;
p:array[1..9]of integer;
c:array[1..9]of boolean;
procedure calc;
begin
if p[1]>p[6] then exit;
if p[6]>p[9] then exit;
if p[4]>p[2] then exit;
if p[5]>p[3] then exit;
if p[7]>p[8] then exit;
if p[1]+p[2]+p[4]+p[6]<>n then exit;
if p[1]+p[3]+p[5]+p[9]<>n then exit;
if p[6]+p[7]+p[8]+p[9]<>n then exit;
inc(r);
end;
procedure dfs(k:longint);
var a:longint;
begin
if k>9 then
begin
calc;
exit;
end;
for a:=1 to 9 do
if not c[a] then
begin
p[k]:=a;
c[a]:=true;
dfs(k+1);
c[a]:=false;
end;
p[k]:=0;
end;
begin
read(n);
dfs(1);
write(r);
end.
var n,r:longint;
p:array[1..9]of integer;
c:array[1..9]of boolean;
procedure calc;
begin
if p[1]>p[6] then exit;
if p[6]>p[9] then exit;
if p[4]>p[2] then exit;
if p[5]>p[3] then exit;
if p[7]>p[8] then exit;
if p[1]+p[2]+p[4]+p[6]<>n then exit;
if p[1]+p[3]+p[5]+p[9]<>n then exit;
if p[6]+p[7]+p[8]+p[9]<>n then exit;
inc(r);
end;
procedure dfs(k:longint);
var a:longint;
begin
if k>9 then
begin
calc;
exit;
end;
for a:=1 to 9 do
if not c[a] then
begin
p[k]:=a;
c[a]:=true;
dfs(k+1);
c[a]:=false;
end;
p[k]:=0;
end;
begin
read(n);
dfs(1);
write(r);
end.
追问
乖乖!

2025-02-09 广告
是的。传统上,对于符合要求的内毒素检测,最终用户必须从标准内毒素库存瓶中构建至少一式两份三点标准曲线;必须有重复的阴性控制;每个样品和PPC必须一式两份。有了Sievers Eclipse内毒素检测仪,这些步骤可以通过使用预嵌入的内毒素标准...
点击进入详情页
本回答由Sievers分析仪提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询