# POJ 2295 – A DP Problem

WA了N多次,猛然发现一处少写一符号,总算A掉了.这种题目就是要细心细心再细心.

``````
#include <iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
char str[1000]={0};
int i=0;
int n=0;
cin>>n;
while (n–)
{
int len=0;
cin>>str;
len=strlen(str);
int sign=1;
int xx[2]={0};
int cc[2]={0};
int cnt=0;//
int stack=0;
int empty=1;
for(i=0;i<len;i++)
{
switch (str[i])
{
case ‘=‘:
if(!empty)
{
cc[cnt]+=stack*sign;
}
empty=1;
stack=0;
cnt++;
sign=1;
break;
case ‘-‘:
if(!empty)
{
cc[cnt]+=stack*sign;
stack=0;
empty=1;
sign=-1;
}
else
sign*=-1;

break;
case ‘+‘:
if(!empty)
{
cc[cnt]+=stack*sign;
stack=0;
sign=1;
empty=1;
}
break;
case ‘x‘:
if(!empty)
{
xx[cnt]+=stack*sign;
stack=0;
empty=1;
}
else
xx[cnt]+=sign;
sign=1;
break;
default:
stack=stack*10+str[i]-‘0‘;
empty=0;
break;
}
}
if(!empty)
{
cc[cnt]+=sign*stack;
}
if(xx[0]==xx[1])
{
if(cc[0]==cc[1])
{
cout<<“IDENTITY“<<endl;
}
else
cout<<“IMPOSSIBLE“<<endl;
}
else
if((cc[1]-cc[0])%(xx[0]-xx[1])==0)
{
cout<<(cc[1]-cc[0])/(xx[0]-xx[1])<<endl;
}
else
{
int flag1,flag2;
if(cc[1]>cc[0])
flag1=1;
else
flag1=-1;
if(xx[0]>xx[1])
flag2=1;
else flag2=-1;
if(flag1*flag2>0)
cout<<(cc[1]-cc[0])/(xx[0]-xx[1])<<endl;
else
cout<<(cc[1]-cc[0])/(xx[0]-xx[1])-1<<endl;
}
}
return 0;
}
``````