YZOI Easy Round 2_化简(simplify.c/cpp/pas)
Description
给定一个多项式,输出其化简后的结果。
Input
一个字符串,只含有关于字母x 的多项式,不含括号与分式,没有多余的空格。
Output
一个字符串,化简后的多项式,按照次数从大到小的顺序输出各项。
Input Sample
x^3+3*x^4-2*x^3+1-x
Output Sample
3*x^4-x^3-x+1
Hint
每项系数<10,次数<6,项数<20。字符串长度不超过100。
很烦的模拟 代码如下:
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=1000+10;
string s;
int a[maxn],lc=1,len;
bool first=true;
int main()
{
// freopen("simplify.in","r",stdin);
// freopen("simplify.out","w",stdout);
cin>>s;
len=s.size();
if(len==1)
{
cout<<s[0];
return 0;
}
for(int i=0;i<len;i++)
{
if(s[i]=='x')
{
if(i==3)
{
if(s[0]=='-')
{
if(s[4]=='^')
{
a[s[5]-'0']-=s[1]-'0';
}
else
{
a[1]-=s[1]-'0';
}
continue;
}
}
if(s[0]>='0'&&s[0]<='9'&&i==2)
{
if(s[3]=='^')
{
a[s[4]-'0']+=s[0]-'0';
}
else
{
a[1]+=s[0]-'0';
}
continue;
}
// if(i==0)
// {
// a[1]++;
// continue;
// }
if(s[i+1]=='^')
{
if(i==0)
{
a[s[i+2]-'0']++;
continue;
}
if(s[i-1]=='*')
{
if(s[i-3]=='+')
a[s[i+2]-'0']+=s[i-2]-'0';
else if(s[i-3]=='-')
a[s[i+2]-'0']-=(s[i-2]-'0');
}
else
{
if(i==0)
{
a[s[i+2]-'0']++;
continue;
} if(s[i-1]=='+'||s[i-1]=='-')
{
if(s[i-1]=='-')
a[s[i+2]-'0']--;
else
a[s[i+2]-'0']++;
}
else
{
a[s[i+2]-'0']--;
}
}
}
else
{
if(s[i-1]=='+')
{
a[1]++;
}
else
{
a[1]--;
}
}
}
else
{
if(s[i]>='0'&&s[i]<='9')
{
if(s[i-1]=='^'||s[i+1]=='*')
continue;
a[0]+=s[i]-'0';
}
}
}
for(int i=0;i<len;i++)
{
if(s[i]=='^')
lc=max(lc,s[i+1]-'0');
}
for(int i=lc;i>=0;i--)
{
if(a[i]==0)
continue;
if(i==0)
{
if(a[i]>0)
{
if(first)
{
cout<<a[i];
first=false;
continue;
}
cout<<'+'<<a[i];
}
else
{
cout<<'-'<<a[i];
}
continue;
}
if(i==1)
{
if(a[i]>0)
{
if(a[i]==1)
{
if(first)
{
cout<<"x";
first=false;
continue;
}
cout<<"+x";
}
else
{
if(first)
{
cout<<a[i]<<"*x";
first=false;
continue;
}
cout<<"+"<<a[i]<<"*x";
}
}
else
{
if(a[i]==-1)
{
cout<<"-x";
}
else
{
cout<<a[i]<<"*x";
}
}
continue;
}
if(a[i]>0)
{
if(a[i]==1)
{
if(first)
{
cout<<"x^"<<i;
first=false;
continue;
}
else
{
cout<<"+x^"<<i;
} }
else
{
if(first)
{
cout<<a[i]<<"*x^"<<i;
first=false;
continue;
}
else
{
cout<<"+"<<a[i]<<"*x^"<<i;
cout<<9999999<<endl;
}
} }
else
{
if(a[i]==-1)
{
cout<<"-x^"<<i;
}
else
{
cout<<a[i]<<"*x^"<<i;
}
}
}
// cout<<endl;
// for(int i=8;i>=0;i--)
// cout<<a[i]<<' ';
// cout<<endl;
// cout<<a[3]<<endl;
return 0;
}
YZOI Easy Round 2_化简(simplify.c/cpp/pas)的更多相关文章
- YZOI Easy Round 2_回文串 string
原文链接:http://laphets1.gotoip3.com/?id=18 Description 给出一个由小写字母组成的字符串,其中一些字母被染黑了,用?表示.已知原来的串不是 一个回文串,现 ...
- HDU 4565 So Easy! 数学 + 矩阵 + 整体思路化简
http://acm.hdu.edu.cn/showproblem.php?pid=4565 首先知道里面那个东西,是肯定有小数的,就是说小数部分是约不走的,(因为b限定了不是一个完全平方数). 因为 ...
- 【mongoDB高级篇②】大数据聚集运算之mapReduce(映射化简)
简述 mapReduce从字面上来理解就是两个过程:map映射以及reduce化简.是一种比较先进的大数据处理方法,其难度不高,从性能上来说属于比较暴力的(通过N台服务器同时来计算),但相较于grou ...
- matlab化简符号表达式
化简符号表达式计算机毕竟还是挺笨的, 经过一系列的符号计算后, 得到的结果可能只有它自己才能看懂, Matlab提供大量函数以用于符号表达式的化简. collect(f): 函数用途是合并多项式中相同 ...
- NOIP201402比例化简
比例化简 [问题描述]在社交媒体上,经常会看到针对某一个观点同意与否的民意调查以及结果.例如,对某一观点表示支持的有 1498 人,反对的有 902 人,那么赞同与反对的比例可以简单的记为1498:9 ...
- 化简复杂逻辑,编写紧凑的if条件语句
当业务逻辑很复杂,涉及多个条件的真假,或者多种条件下都会执行同一动作时,如何编写紧凑的if语句呢?本文借由一个实际例子,利用数学的布尔逻辑整理条件,最终产生if语句. 问题 在<X3 重聚> ...
- 《Linear Algebra and Its Application》-chaper1-行化简法解决线性方程组
在实际生产生活中,需要我们解大量的线性方程组,例如是有探测.线性规划.电路等,这里我们便从理论角度建立一套解决线性方程组的体系. 线性方程组: 形如下面形式的方程组称为线性方程组. 回想起解决二元线性 ...
- poj3708:函数式化简+高精度进制转换+同余方程组
题目大意 给定一个函数 找出满足条件 等于 k 的最小的x m,k,d已知 其中 m,k 很大需要使用高精度存储 思路: 对 函数f(m)进行化简 ,令t=ceil( log(d,m) ) 可以得 ...
- 化简复杂逻辑,编写紧凑的if条件语句(二):依据if子句顺序化简条件
<化简复杂逻辑,编写紧凑的if条件语句>已经得出了跳.等.飞.异常的各自条件,方便起见这里重新贴一下. 立即跃迁:!a && b && d 等待跃迁:!a ...
随机推荐
- How to effectively work with multiple files in Vim?
Why not use tabs (introduced in Vim 7)? You can switch between tabs with :tabn and :tabp, With :tabe ...
- opencv Installation in Linux and hello world
http://opencv.org/quickstart.html Installation in Linux These steps have been tested for Ubuntu 10.0 ...
- yii CListView中使用CArrayDataProvider自定义数组作为数据
CArrayDataProvider类手册: http://www.yiichina.com/api/CArrayDataProvider 在yii中无论是CListView还是CGridView,对 ...
- bash的多行注释
:<<EOF 注释的代码... EOF 单行是#
- C# 自定义事件
C#自定义事件和java有所不同,涉及到委托.下面代码包括自定义事件从事件定义到事件触发和执行的全过程. using System; using System.Collections.Generic; ...
- WebService基于SoapHeader实现安全认证(一)
本文转载:http://www.cnblogs.com/houleixx/archive/2009/08/22/webservice-soapheader-security.html WebServi ...
- Uncaught TypeError: Cannot read property 'post' of undefined
- Centos安装成功mysql-5.5.37
摘自http://www.2cto.com/os/201404/296364.html(原文请关注) 这几天在centos下装mysql,这里记录一下安装的过程,方便以后查阅 Mysql5.5.37安 ...
- [Javascript + rxjs] Using the map method with Observable
Like an array, Observable has a map method that allows us to transform a sequence into a new Observa ...
- oracle12 pl/sql
pl/sql块介绍 介绍 块(block)是pl/sql的基本程序单元,编写pl/sql程序实际上就是编写pl/sql块,要完成相对简单的应用功能,可能只需要编写一个pl/sql块,但是如果想要实 ...