您的位置:首页 > 其它

Zju2492 A Dp Problem

2013-11-06 19:35 295 查看
给你一些一元一次方程,让你求出未知数的解,如果只有一个解则输出这个解,如果有无数个则输出"IDENTITY"(如 x=x),无解则输出“IMPOSSIBLE”(如 x+2=x+3)。

考虑等式两边也就是“=”两边,分别求出两边未知数项的系数和常数项,分别为,f1,d1,f2,d2,那么最后的答案就是(d2-d1)/(f1-f2)。(这个。。。大家应该都懂!!!)。

处理系数有点恶心。

code:

 

var
s:ansistring;
n,i,j,k,f1,d1,f2,d2,tmp,x,t,code,tot:longint;
ch,cc:char;
begin
readln(tot);
while tot>0 do
begin
readln(s);
for cc:='a' to 'z' do
if pos(cc,s)<>0 then
begin
ch:=cc; break;
end;
n:=pos('=',s);
i:=1; x:=1; f1:=0; f2:=0; d1:=0; d2:=0; tmp:=0;
while (i<n) do
begin
if (s[i]=ch) and ((i=1) or (s[i-1] in ['+','-'])) then
begin inc(i); inc(f1,x);  continue; end;
if s[i]=ch then
begin
inc(i);
inc(f1,x*tmp);
tmp:=0;
continue;
end;
inc(d1,x*tmp);
if s[i]='-' then begin inc(i); x:=-1; tmp:=0; continue; end;
if s[i]='+' then begin inc(i); x:=1; tmp:=0; continue; end;
tmp:=0;
repeat
if (s[i] in [ch,'+','-','='] )then break;
val(s[i],t,code);
tmp:=tmp*10+t;
inc(i);
until false;
end;
inc(d1,x*tmp);
i:=n+1; x:=1;
tmp:=0;
while i<=length(s) do
begin
if (s[i]=ch) and ((i=n+1) or (s[i-1] in['+','-'])) then
begin inc(i); inc(f2,x); continue; end;
if s[i]=ch then
begin
inc(i);
inc(f2,x*tmp);
tmp:=0;
continue;
end;
inc(d2,x*tmp);
if s[i]='-' then begin inc(i); x:=-1; tmp:=0; continue; end;
if s[i]='+' then begin inc(i); x:=1;  tmp:=0; continue; end;
tmp:=0;
repeat
if (i=length(s)+1) then break;
if s[i] in [ch,'-','+'] then break;
val(s[i],t,code);
tmp:=tmp*10+t;
inc(i);
until false;
end;
inc(d2,tmp*x);
if (d2-d1=0) and (f1-f2=0) then writeln('IDENTITY')
else
if (f1-f2=0) then writeln('IMPOSSIBLE')
else

if (d2-d1)/(f1-f2)=0 then writeln(0)

else
writeln((d2-d1)/(f1-f2):0:0);
dec(tot);
end;
end.


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: