记录编号 16095 评测结果 TTTTTT
题目名称 多项式运算 最终得分 0
用户昵称 Gravatar苏轼 是否通过 未通过
代码语言 C++ 运行时间 6.000 s
提交时间 2010-04-19 17:38:45 内存使用 0.26 MiB
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

const int MAXN=500;

struct Node
{
	int x,y;	//x*x^y
};

int n;
char ch[5000];

bool comp(Node a,Node b)
{
	return (a.y<b.y);
}

bool change(vector<Node> &v)
{
	int now=0,sign=1;
	unsigned p=0;
	bool get=false,used=false;
	scanf("%s",ch);
	if (strcmp(ch,"-1")==0) return false;
	while (p<strlen(ch))
	{
		if (ch[p]>='0'&&ch[p]<='9')
		{
			used=false;
			get=true;
			now=now*10+ch[p]-'0';
			p++;
			continue;
		}
		if (ch[p]=='x')
		{
			if (ch[++p]=='^')
			{
				used=true;
				if (!get)now=1;
				int zh=0;
				while (ch[p+1]>='0'&&ch[p+1]<='9')
					zh=zh*10+ch[++p]-'0';
				p++;
				Node temp={sign*now,zh};
				v.push_back(temp);
				now=0;
			}
			else
			{
				used=true;
				if (!get)now=1;
				Node temp={sign*now,1};
				v.push_back(temp);
				now=0;
			}
			continue;
		}
		if (ch[p]=='+')
		{
			sign=1;p++;continue;
		}
		if (ch[p]=='-')
		{
			sign=-1;p++;continue;
		}
	}
	if (now!=0&&!used)
	{
		Node temp={sign*now,0};
		v.push_back(temp);
	}
	return true;
}

void evaluate()
{
	int x;
	vector<Node> v;
	scanf("%d",&x);
	change(v);
	long long ans=0;
	while (!v.empty())
	{
		Node now=v.back();
		v.pop_back();
		ans+=now.x*pow(double(x),double(now.y));
	}
	printf("%d\n",ans);
}

void add()
{
	vector<Node> ans;
	bool first=true,out=false;
	while (change(ans));
	sort(ans.begin(),ans.end(),comp);
	Node now;
	while (!ans.empty())
	{
		now=ans.back();
		ans.pop_back();
		while(!ans.empty())
		{
			Node temp=ans.back();
			if (temp.y==now.y)
			{
				now.x+=temp.x;
				ans.pop_back();
			}
			else 
			{
				if (first)
				{
					if (now.x==0)break;
					out=true;
					if (now.y==0&&now.x!=0)
					{
						printf("%d",now.x);
						break;
					}
					else
					{
						if (now.x!=1)printf("%d",now.x);
						printf("x");
						if (now.y>1)printf("^%d",now.y);
						first=false;
					}
				}
				else
				{
					if (now.x==0)break;
					out=true;
					if (now.x>0)printf("+");
					if (now.y==0&&now.x!=0)
					{
						printf("%d",now.x);
						break;
					}
					else
					{
						if (now.x!=1)printf("%d",now.x);
						printf("x");
						if (now.y>1)printf("^%d",now.y);
						first=false;
					}			
				}
				break;
			}
		}
	}
	if (first&&now.x!=0)
	{
		out=true;
		if (now.y==0&&now.x!=0)
		{
			printf("%d",now.x);
		}
		else
		{
			if (now.x!=1)printf("%d",now.x);
			printf("x");
			if (now.y>1)printf("^%d",now.y);
			first=false;
		}
	}
	else
	{
		out=true;
		if (now.x>0)printf("+");
		if (now.y==0&&now.x!=0)
		{
			printf("%d",now.x);
		}
		else
		{
			if (now.x!=1)printf("%d",now.x);
			printf("x");
			if (now.y>1)printf("^%d",now.y);
			first=false;
		}			
	}
	if (!out)printf("0");
	printf("\n");
}

void substrack()
{
	vector<Node> ans,mul;
	bool first=true;
	change(ans);
	while (change(mul));
	for(unsigned i=0;i<mul.size();i++)
	{
		Node temp=mul[i];
		temp.x=-temp.x;
		mul.push_back(temp);
	}
	sort(ans.begin(),ans.end(),comp);
	Node now;
	bool out=false;
	while (!ans.empty())
	{
		now=ans.back();
		ans.pop_back();
		while(!ans.empty())
		{
			Node temp=ans.back();
			if (temp.y==now.y)
			{
				now.x+=temp.x;
				ans.pop_back();
			}
			else 
			{
				if (first)
				{
					if (now.x==0)break;
					out=true;
					if (now.y==0&&now.x!=0)
					{
						printf("%d",now.x);
						break;
					}
					else
					{
						if (now.x!=1)printf("%d",now.x);
						printf("x");
						if (now.y>1)printf("^%d",now.y);
						first=false;
					}
				}
				else
				{
					if (now.x==0)break;
					out=true;
					if (now.x>0)printf("+");
					if (now.y==0&&now.x!=0)
					{
						printf("%d",now.x);
						break;
					}
					else
					{
						if (now.x!=1)printf("%d",now.x);
						printf("x");
						if (now.y>1)printf("^%d",now.y);
						first=false;
					}			
				}
				break;
			}
		}
	}
	if (first&&now.x!=0)
	{
		out=true;
		if (now.y==0&&now.x!=0)
		{
			printf("%d",now.x);
		}
		else
		{
			if (now.x!=1)printf("%d",now.x);
			printf("x");
			if (now.y>1)printf("^%d",now.y);
			first=false;
		}
	}
	else
	{
		out=true;
		if (now.x>0)printf("+");
		if (now.y==0&&now.x!=0)
		{
			printf("%d",now.x);
		}
		else
		{
			if (now.x!=1)printf("%d",now.x);
			printf("x");
			if (now.y>1)printf("^%d",now.y);
			first=false;
		}			
	}
	if (!out)printf("0");
	printf("\n");
}

void multiply()
{
	bool first=true;
	vector<Node> ans,v2;
	change(ans);
	while (change(v2))
	{
		vector<Node> temp;
		for(unsigned i=0;i<ans.size();i++)
			for(unsigned j=0;j<v2.size();j++)
			{
				Node now={ans[i].x*v2[j].x,ans[i].y+v2[j].y};
				temp.push_back(now);
			}
		ans=temp;
	}
	sort(ans.begin(),ans.end(),comp);
	Node now;
	bool out=false;
	while (!ans.empty())
	{
		now=ans.back();
		ans.pop_back();
		while(!ans.empty())
		{
			Node temp=ans.back();
			if (temp.y==now.y)
			{
				now.x+=temp.x;
				ans.pop_back();
			}
			else 
			{
				if (first)
				{
					if (now.x==0)break;
					out=true;
					if (now.y==0&&now.x!=0)
					{
						printf("%d",now.x);
						break;
					}
					else
					{
						if (now.x!=1)printf("%d",now.x);
						printf("x");
						if (now.y>1)printf("^%d",now.y);
						first=false;
					}
				}
				else
				{
					if (now.x==0)break;
					out=true;
					if (now.x>0)printf("+");
					if (now.y==0&&now.x!=0)
					{
						printf("%d",now.x);
						break;
					}
					else
					{
						if (now.x!=1)printf("%d",now.x);
						printf("x");
						if (now.y>1)printf("^%d",now.y);
						first=false;
					}			
				}
				break;
			}
		}
	}
	if (first&&now.x!=0)
	{
		out=true;
		if (now.y==0&&now.x!=0)
		{
			printf("%d",now.x);
		}
		else
		{
			if (now.x!=1)printf("%d",now.x);
			printf("x");
			if (now.y>1)printf("^%d",now.y);
			first=false;
		}
	}
	else
	{
		out=true;
		if (now.x>0)printf("+");
		if (now.y==0&&now.x!=0)
		{
			printf("%d",now.x);
		}
		else
		{
			if (now.x!=1)printf("%d",now.x);
			printf("x");
			if (now.y>1)printf("^%d",now.y);
			first=false;
		}			
	}
	if (!out)printf("0");
	printf("\n");
}

int main()
{
	freopen("poly.in","r",stdin);
	freopen("poly.out","w",stdout);
	char command[10];
	gets(command);
	int p=1;
	while (strcmp(command,"last")!=0)
	{
		if (strcmp(command,"evaluate")==0)
		{
			printf("%d:",p++);
			evaluate();
		}
		else if (strcmp(command,"add")==0)
		{
			printf("%d:",p++);
			add();
		}
		else if (strcmp(command,"subtract")==0)
		{
			printf("%d:",p++);
			substrack();
		}
		else if (strcmp(command,"multiply")==0)
		{
			printf("%d:",p++);
			multiply();
		}
		gets(command);
	}
}