Zju2492 A Dp Problem

给你一些一元一次方程,让你求出未知数的解,如果只有一个解则输出这个解,如果有无数个则输出"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.

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值