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

简单的栈操作 zero可以直接忽略

Posted by xuchang at 2010-12-13 12:08:04 on Problem 2121
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include<stack>
using namespace std;


int main()
{
	map<string,int> value;
	stack<long> fstk;
	value["one"]=1;
	value["two"]=2;
	value["three"]=3;
	value["four"]=4;
	value["five"]=5;
	value["six"]=6;	
	value["seven"]=7;
	value["eight"]=8;
	value["nine"]=9;
	value["ten"]=10;
	value["eleven"]=11;
	value["twelve"]=12;
	value["thirteen"]=13;
	value["fourteen"]=14;
	value["fifteen"]=15;
	value["sixteen"]=16;
	value["seventeen"]=17;
	value["eighteen"]=18;
	value["nineteen"]=19;
	value["twenty"]=20;
	value["thirty"]=30;
	value["forty"]=40;
	value["fifty"]=50;
	value["sixty"]=60;
	value["seventy"]=70;
	value["eighty"]=80;
	value["ninety"]=90;
	value["hundred"]=100;
	value["thousand"]=1000;
	value["million"]=1000000;
	vector<string> v;
	while(true)
	{
		bool neg=false;
		string ss;
		getline(cin,ss);
		if(ss.empty())
			break;
		char* p=const_cast<char*>(ss.c_str());
		v.push_back(strtok(p," "));
		char* temp;
		while(temp=strtok(NULL," "))
		{
			if(temp!="zero")
				v.push_back(temp);
		}
		int start=0,end=v.size();
		if(v[0]=="negative")
		{
			neg=true;
			start=1;
		}
		long res=0;
		while(start!=end)
		{
			if(fstk.empty()||fstk.top()>value[v[start]])
			{
				fstk.push(value[v[start]]);
			}
			else
			{
				long tv=0;
				while(!fstk.empty()&&fstk.top()<value[v[start]])
				{
					tv=tv+fstk.top();
					fstk.pop();
				}
				fstk.push(tv*value[v[start]]);
			}
			start++;
		}
		while(!fstk.empty())
		{
				res+=fstk.top();
				fstk.pop();
		}
		if(neg)
			res=res-2*res;
		cout<<res<<endl;
		v.clear();
	}
	return 0;
}


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