题目地址:http://codevs.cn/problem/1017/
分析:
数据比较弱,所以深搜也能轻松AC
代码:
program ex1017;
var n,k,i:integer;
t,ans:int64;
a:array[1..40] of char;
function s(sta,en:integer):int64;//这个函数用来计算从第sta到第en位的数字
var i:integer;
st:string;
begin
st:='';
for i:=sta+1 to en do
st:=st+a[i];
val(st,s);
end;
procedure search(num,l:integer);//num代表放第几个乘号,l代表上一个乘号的位置
var i:integer;
j:int64;
begin
if num>k then
begin
j:=s(l,n);
if j<>0 then//如果剩下的数等于0就不用做了,因为乘积总为0
begin
t:=t*j;
if t>ans then
ans:=t;
t:=t div j;//这里也要回溯一下
end;
end
else
begin
for i:=l+1 to n-k+num do//该乘号能放的位置
begin
j:=s(l,i);
if j<>0 then
begin
t:=t*j;//保存当前结果
search(num+1,i);//深搜下一层
t:=t div j;//回溯
end;
end;
end;
end;
begin
readln(n,k);
for i:=1 to n do
read(a[i]);//每个数字都以字符形式读入
ans:=0;//最终结果
t:=1;//打擂台用的临时量
search(1,0);//放第一个乘号,假设上一个乘号放在0位置(其实没有上一个乘号)
writeln(ans);
end.