Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

改过了还WA,岂有此理

Posted by Ethanh at 2005-03-16 17:28:29 on Problem 2295
In Reply To:很明显,你忽略了IMPOSSIBLE的情况... Posted by:ferret_chao at 2005-03-16 17:08:18
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
	int n,x,y,result;
	int num=0;
	char ch,ch2,flag;
	char finish=0;
	string str;
	
	cin>>n;
	while(n-->0){
		x=0,y=0;
		result=0;
		flag=1;
		finish=0;
		
        cin>>str;
		istringstream cins(str);
		
		while(!finish&&cins.get(ch)){
			
			switch(ch){
			case 'x':// read x
				if(flag)
					x++;
				else x--;
				break;

			case '+':// read '+'
				cins.get(ch);
				if(ch=='x'){
					if(flag)
						x++;
					else x--;
					break;
				}
				cins.putback(ch);
				if(isdigit(ch)){
					cins>>num;
					cins.get(ch2);
					if(ch2!='x'){
						cins.putback(ch2);
						if(flag)
							y+=num;
						else y-=num;
					}
					else{
						if(flag)
							x+=num;
						else x-=num;
					}
					break;
				}
				else{
					x=0;
					finish=1;//impossible
					continue;
				}
			
			case '-'://read '-'
				cins.get(ch);
				if(ch=='x'){
					if(flag)
						x--;
					else x++;
					break;
				}
				cins.putback(ch);
				if(isdigit(ch)){
					cins>>num;
					cins.get(ch2);
					if(ch2!='x'){
						cins.putback(ch2);
						if(flag)
							y-=num;
						else y+=num;
					}
					else{
						if(flag)
							x-=num;
						else x+=num;
					}
					break;
				}
				else{
					x=0;
					y=1;
					finish=1;//impossible
					continue;
				}

			case '=':// read '='
				flag=0;
				break;
			
			case '0':case '1':case '2':case '3':case '4':case '5':
			case '6':case '7':case '8':case '9':
				cins.putback(ch);
				cins>>num;
				cins.get(ch2);
				if(ch2!='x'){
					cins.putback(ch2);
					if(flag)
						y+=num;
					else y-=num;
				}
				else{
					if(flag)
						x+=num;
					else x-=num;
				}
				break;
			default:
              x=0;
			  y=1;
			  finish=1;//impossible
			}
		}
		if(!x){
			if(!y)
				cout<<"IDENTITY"<<endl;
			else cout<<"IMPOSSIBLE"<<endl;
			continue;
		}
        result=0-y/x;
		cout<<result<<endl;
	}
	return 1;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator