A DP Problem poj 2295

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;
}

posted on 2010-08-21 17:24 若余 阅读(206) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2010年8月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

统计

常用链接

留言簿

随笔档案(16)

搜索

最新随笔

最新评论

评论排行榜