主题:[转帖]数字游戏2
uses crt;
var a:array[1..32767]of integer;
n,ml,m:integer;
s,s1,s2,s3,so:string;
g,i,j,q,l,k,g1:integer;
function d(j:integer):integer;
begin
repeat
j:=j+1;
until(not(s[j] in ['0'..'9']));
d:=j;
end;
begin
clrscr;
s:='';
write('n=');
readln(n);
for ml:=1 to n do begin
readln(a[ml]);
str(a[ml],s1);
if(a[ml]>=0)and(ml<>1)then begin
s:=s+'+'+s1;
end else s:=s+s1;
end;
write('m=');
s1:=s;
readln(m);
g1:=0;
k:=0;
for i:=1 to length(s) do begin
for j:=i+1 to length(s) do begin
if(s[i]='-')or(s[i]='+')then begin
if(d(j)-j=1)then begin
insert('(',s,i+1);
insert(')',s,j+2);
so:=s;
for q:=pos('(',s)+1 to pos(')',s)-1 do if s[q]='+' then s[q]:='-' else
if s[q]='-' then s[q]:='+';
delete(s,pos('(',s),1);delete(s,pos(')',s),1);
s:=s+'+';
g:=0;
repeat
inc(k);
if(s[k]='+')or(s[k]='-')then begin
s3:=copy(s,1,k-1);
val(s3,l);
g:=g+l;
delete(s,1,k-1);
if k<>1 then k:=0;
end;
until s='+';
if g=m then begin
writeln(so,'=',m);
readln;
end else if g<>m then s:=s1;
end;
end;
end;
end;
if(j=length(s))or(i=length(s))then writeln('No answer');
end.
var a:array[1..32767]of integer;
n,ml,m:integer;
s,s1,s2,s3,so:string;
g,i,j,q,l,k,g1:integer;
function d(j:integer):integer;
begin
repeat
j:=j+1;
until(not(s[j] in ['0'..'9']));
d:=j;
end;
begin
clrscr;
s:='';
write('n=');
readln(n);
for ml:=1 to n do begin
readln(a[ml]);
str(a[ml],s1);
if(a[ml]>=0)and(ml<>1)then begin
s:=s+'+'+s1;
end else s:=s+s1;
end;
write('m=');
s1:=s;
readln(m);
g1:=0;
k:=0;
for i:=1 to length(s) do begin
for j:=i+1 to length(s) do begin
if(s[i]='-')or(s[i]='+')then begin
if(d(j)-j=1)then begin
insert('(',s,i+1);
insert(')',s,j+2);
so:=s;
for q:=pos('(',s)+1 to pos(')',s)-1 do if s[q]='+' then s[q]:='-' else
if s[q]='-' then s[q]:='+';
delete(s,pos('(',s),1);delete(s,pos(')',s),1);
s:=s+'+';
g:=0;
repeat
inc(k);
if(s[k]='+')or(s[k]='-')then begin
s3:=copy(s,1,k-1);
val(s3,l);
g:=g+l;
delete(s,1,k-1);
if k<>1 then k:=0;
end;
until s='+';
if g=m then begin
writeln(so,'=',m);
readln;
end else if g<>m then s:=s1;
end;
end;
end;
end;
if(j=length(s))or(i=length(s))then writeln('No answer');
end.