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 ...
随机推荐
- python学习笔记(四)之分支和循环
python中比较操作符有: > >= < <= == != 这些操作符返回True或False 1 >>> 1 < 3 2 True 3 >&g ...
- 集合类HashMap,HashTable,ConcurrentHashMap区别?
1.HashMap 简单来说,HashMap由数组+链表组成的,数组是HashMap的主体,链表则是主要为了解决哈希冲突而存在的,如果定位到的数组位置不含链表(当前entry的next指向null), ...
- css优先级机制
所谓CSS优先级,即是指CSS样式在浏览器中被解析的先后顺序. 1.important >(内联样式)Inline style >(内部样式)Internal style sheet ...
- js_!和!!的使用
js中有些特殊的数据(“” 0 null undefined NaN),请求后台返回的数据中往往都有一些这样的数据,需要对这些数据进行过滤. 过滤代码 var a = 0;//0 "&quo ...
- ie6下双边距的问题
1.ie6双边距情况 <div class="red"></div> <div class="blue"></div& ...
- perl6中的hash定义(1)
,,,); say %hash; , b => ); say %hash2; my %hash3 = (:name('root'), :host('localost')); say %hash3 ...
- vs 预编译命令行
xcopy "$(SolutionDir)\Transight_FY_DataExchange_UI\CuscapiUpdaterServer.xml" /i /d /y
- 【洛谷P3651】展翅翱翔之时
难以吐槽出题人的中二病…… 这题有点类似ZJOI2008 骑士,先跑树上的,最后拆环即可. #include<bits/stdc++.h> #define N 100005 typedef ...
- 2017多校第8场 HDU 6138 Fleet of the Eternal Throne AC自动机或者KMP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给n个串,每次询问x号串和y号串的最长公共子串的长度,这个子串必须是n个串中某个串的前缀 ...
- maven使用备忘
maven的所有功能本质上都是通过插件来实现的所有的功能.archetype插件就是根据项目类型创建项目的插件.执行archetype:generate命令就会list一系列的项目类型,可以选择一个合 ...