Polynomial Problem(hdu 1296 表达式求值)
2017-04-02 15:22
323 查看
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.
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; }
相关文章推荐
- HDU 1296 (表达式求值)
- Polynomial Problem( HDU - 1296 表达式求值)
- 【模拟题、表达式求值】hdu hdoj 1296 Polynomial Problem
- hdu 3596 表达式求值
- 【表达式求值】HDU 4192
- HDU 6145 Arithmetic of Bomb II or Aizu 2789 Compressed Formula 表达式展开求值 矩阵快速幂
- HDU 1237(表达式求值)
- HDU 1237 简单计算器 表达式求值
- HDU 1237 表达式求值
- hdu 1237(中序表达式求值)
- hdu 1237 简单计算器(表达式求值)
- HDU 1237(简单计算器)栈的应用-表达式求值
- HDU 1237 简单计算器(不带括号 字符间有空格的表达式求值)
- hdu 4192(表达式求值)
- HDU-4192 Guess the Numbers 表达式求值
- hdu 1082 Matrix Chain Multiplication--表达式求值
- hdu 1237 简单计算器【最简单的表达式求值】
- HDU 1237 - 简单计算器 表达式求值
- 表达式求值
- 栈表达式求值程序