Polynomial Problem(hdu 1296 表达式求值)
We have learned how to obtain the value of a polynomial when we were a middle school student. If f(x) is a polynomial of degree n, we can let
If we have x, we can get f(x) easily. But a computer can not understand the expression like above. So we had better make a program to obtain f(x).
Input
There are multiple cases in this problem and ended by the EOF. In each case, there are two lines. One is an integer means x (0<=x<=10000), the other is an expression means f(x). All coefficients ai(0<=i<=n,1<=n<=10,-10000<=ai<=10000) are integers. A correct expression maybe likes
1003X^5+234X^4-12X^3-2X^2+987X-1000
Output
For each test case, there is only one integer means the value of f(x).
Sample Input
3
1003X^5+234X^4-12X^3-2X^2+987X-1000
Sample Output
264302
Notice that the writing habit of polynomial f(x) is usual such as
X^6+2X^5+3X^4+4X^3+5X^2+6X+7
-X^7-5X^6+3X^5-5X^4+20X^3+2X^2+3X+9
X+1
X^3+1
X^3
-X+1 etc. Any results of middle process are in the range from -1000000000 to 1000000000.
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include <ctype.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=110000;
typedef long long ll;
int main()
{
char str[maxn];
int flag,mul,ans,x;
while(~scanf("%d",&x))
{
scanf("%s",str);
int len=strlen(str);
int num=-INF;
flag=1;
ans=0;
for(int i=0; i<len; i++)
{
if(str[i]=='-')
{
flag=0;
if(i+1<len&&str[i+1]=='X') num=1;
continue;
}
if(str[i]=='+')
{
flag=1;
if(i+1<len&&str[i+1]=='X') num=1;
continue;
}
if(str[i]=='X')
{
if(i-1<0) num=1;
if(!flag) num=-num;
flag=1;
if((i+1<len&&str[i+1]!='^')||i+1>=len)
ans+=num*x,num=-INF;
continue;
}
if(i-1>=0&&str[i]>='0'&&str[i]<='9'&&str[i-1]=='^')
{
if(i+1<len&&str[i+1]>='0'&&str[i+1]<='9')
{
mul=(str[i]-'0')*10+str[i+1]-'0';
i++;
}
else mul=str[i]-'0';
if(!flag) num=-num;
flag=1;
int mid=x;
for(int j=2; j<=mul; j++)
{
mid*=x;
}
ans+=num*mid;
num=-INF;
continue;
}
if(str[i]>='0'&&str[i]<='9')
{
if(num==-INF) num=str[i]-'0';
else num=num*10+str[i]-'0';
}
}
if(num!=-INF)
{
if(!flag) num=-num;
ans+=num;
}
printf("%d\n",ans);
}
return 0;
}
Polynomial Problem(hdu 1296 表达式求值)的更多相关文章
- FZU2215 Simple Polynomial Problem(中缀表达求值)
比赛时没做出这题太可惜了. 赛后才反应过来这就是个中缀表达式求值,数字栈存的不是数字而是多项式. 而且,中缀表达式求值很水的,几行就可以搞定. #include<cstdio> #incl ...
- hdu 4192 (表达式求值)
<题目链接> <转载于 >>> > 题目大意: 给你n个数,和一个最终的结果,再给你一个含有n个不同变量的式子,问你这个式子最终能否得到指定的答案. 解题分 ...
- 随手练——HDU 1237 表达式求值(输入格式典型)
坑了老子半天,结果是 float 范围不够!!! 基本思想: 开一个符号栈,一个数字栈: 碰到数字就入栈,碰到符号就与栈顶符号进行对比,如果当前符号优先级小于栈顶符号,数字栈弹出两个数进行栈顶符号运算 ...
- hdu 1237 简单计算器 (表达式求值)【stack】
<题目链接> 题目大意: 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符, ...
- Matrix Chain Multiplication(表达式求值用栈操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...
- 用Python3实现表达式求值
一.题目描述 请用 python3 编写一个计算器的控制台程序,支持加减乘除.乘方.括号.小数点,运算符优先级为括号>乘方>乘除>加减,同级别运算按照从左向右的顺序计算. 二.输入描 ...
- 数据结构--栈的应用(表达式求值 nyoj 35)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=35 题目: 表达式求值 时间限制:3000 ms | 内存限制:65535 KB描述 AC ...
- HNU 12817 Shipura(表达式求值)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12817 解题报告:定义两种运算符号,一种是>>,就 ...
- CF552E 字符串 表达式求值
http://codeforces.com/contest/552/problem/E E. Vanya and Brackets time limit per test 1 second memor ...
随机推荐
- JMeter 保持sessionId
因项目需要,这几天用到了jmeter进行性能测试,测试的是一个管理系统,需要用户先登录,然后才能做操作的,其中就遇到了关于session的问题. 我使用的是badboy(版本2.1)进行的脚本录制,然 ...
- Oracle 导出空表的新方法(彻底解决)
背景 使用Exp命令在oracle 11g 以后不导出空表(rowcount=0),是最近在工作中遇到一个很坑的问题,甚至已经被坑了不止一次,所以这次痛定思痛,准备把这个问题彻底解决.之所以叫新方法, ...
- [bzoj3993][SDOI2015]星际战争-二分+最大流
Brief Description 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团的阵地,其中第i个巨型机器人的装甲值为Ai. ...
- 转载:WebView
前言 现在很多App里都内置了Web网页(Hyprid App),比如说很多电商平台,淘宝.京东.聚划算等等,如下图 那么这种该如何实现呢?其实这是Android里一个叫WebView的组件实现的.今 ...
- Eclipse svn 忽略文件夹/ svn 设置不同步
Eclipse 开发中我们经常用到SVN插件, 但是对于某些文件的缓存来说, 我们只要有操作缓存便会保存一次, 每次提交很是麻烦, 小编这里教你一种不同步某个文件夹的方法 工具/原料 MyEclips ...
- linux信号Linux下Signal信号太详细了,终于找到了
linux信号Linux下Signal信号太详细了,终于找到了 http://www.cppblog.com/sleepwom/archive/2010/12/27/137564.html
- C 实现一个简易的Http服务器 (二)
正文 - 直接搞起 C 实现一个简易的Http服务器 很久以前写过一个简易的http服务器, 后面和一个朋友交流, 反思后发现问题不少.在这里简单搞一下. 让其更加简单去表现httpd本质, 弱化协议 ...
- 1.ubuntu的安装
分两种 1. 在VMware中安装,则与Centos的安装类似 2. 在VirtualBox里安装 --> 1. 先“新建” 一个虚拟电脑 2. 根据需求编辑虚拟电脑的信息 (具体的大小.内存等 ...
- socket实现udp与tcp通信-java
1.简单介绍Socket Socket套接字 网络上具有唯一标识的IP地址和端口号组合在一起才能构成唯一能识别的标识符套接字. 通信的两端都有Socket. 网络通信其实就是Socket间的通信. 数 ...
- 自动关闭IO流-jdk1.7版本
public static void main(String[] args) throws IOException { try( FileInputStream fis = new FileInput ...