求程序设计解题过程,题目如下,谢谢

2025-02-22 23:29:56
推荐回答(2个)
回答1:

说明
乔治取出几支相同长度的棍子,把它们随机切成几段,直到所有小棍都变成50个单位长。现在,他想将棍子变为原来的状态,但他忘了他原来有多少根棍子,棍子原来有多长。请帮他设计一个程序,计算出棍子最小的原长度。所有长度以大于零的整型单位表示。

输入
该输入包含两行。第一行包含切割后的棍子数,最多64根棍子。第二行包含这些棍子的长度/间隔。文件的最后一行包含零。

输出
每行输出应当包含棍子最小原始长度,

输入的例子

9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0

输出的例子

6
5

中心欧洲 1995

回答2:

program dsfa;
var
i,j,k,l,x:integer;
a:array[1..64]of integer;
n,m,o:integer;
ans,asn:boolean;
v:array[1..100]of boolean;
procedure sort(i,j:integer);
var x,y,mid,z:integer;
begin
x:=i;
y:=j;
mid:=(i+j)div 2;
repeat
while a[x]>a[mid] do
inc(x);
while a[y] dec(y);
if x<=y then
begin
z:=a[x];
a[x]:=a[y];
a[y]:=z;
inc(x);
dec(y);
end;
until x>y;
if x if y>i then sort(i,y);
end;
procedure dfs(res,part,rest:integer); //l sheyuanlaichangdu, rest,shengxideduanshu
var j,k:integer;
begin
if res=i then
begin
res:=0;
inc(part);
end;
if (part=m div i) then
begin
x:=i;
ans:=true;
exit;
end;
if rest begin
asn:=true;
exit;
end;
for j:=(part+1) to n do
begin
if v[j] and (res+a[j]<=i) then
begin
v[j]:=false;
if ans then exit;
rest:=rest-a[j];
dfs(res+a[j],part,rest);
if asn then exit;
rest:=rest+a[j];
v[j]:=true;
end;
end;
end;
begin
reset(input);
rewrite(output);
repeat
readln(n);
if n=0 then
break;
m:=0;
o:=0;
fillchar(v,sizeof(v),true);
for i:= 1 to n do
begin
read(a[i]);
m:=m+a[i];
if a[i]>o then
o:=a[i];
end;
sort(1,n);
i:=o;
l:=1;
for i:= o to m do
begin
if m mod i=0 then
begin
ans:=false;
asn:=false;
dfs(0,0,m);
if ans then
begin
writeln(i);
break;
end;
end;
end;
until n=0;
end.