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 表达式求值)的更多相关文章

  1. FZU2215 Simple Polynomial Problem(中缀表达求值)

    比赛时没做出这题太可惜了. 赛后才反应过来这就是个中缀表达式求值,数字栈存的不是数字而是多项式. 而且,中缀表达式求值很水的,几行就可以搞定. #include<cstdio> #incl ...

  2. hdu 4192 (表达式求值)

    <题目链接> <转载于 >>>  > 题目大意: 给你n个数,和一个最终的结果,再给你一个含有n个不同变量的式子,问你这个式子最终能否得到指定的答案. 解题分 ...

  3. 随手练——HDU 1237 表达式求值(输入格式典型)

    坑了老子半天,结果是 float 范围不够!!! 基本思想: 开一个符号栈,一个数字栈: 碰到数字就入栈,碰到符号就与栈顶符号进行对比,如果当前符号优先级小于栈顶符号,数字栈弹出两个数进行栈顶符号运算 ...

  4. hdu 1237 简单计算器 (表达式求值)【stack】

    <题目链接> 题目大意: 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值.  Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符, ...

  5. Matrix Chain Multiplication(表达式求值用栈操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...

  6. 用Python3实现表达式求值

    一.题目描述 请用 python3 编写一个计算器的控制台程序,支持加减乘除.乘方.括号.小数点,运算符优先级为括号>乘方>乘除>加减,同级别运算按照从左向右的顺序计算. 二.输入描 ...

  7. 数据结构--栈的应用(表达式求值 nyoj 35)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=35 题目: 表达式求值 时间限制:3000 ms | 内存限制:65535 KB描述 AC ...

  8. HNU 12817 Shipura(表达式求值)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12817 解题报告:定义两种运算符号,一种是>>,就 ...

  9. CF552E 字符串 表达式求值

    http://codeforces.com/contest/552/problem/E E. Vanya and Brackets time limit per test 1 second memor ...

随机推荐

  1. 【poj1830-开关问题】高斯消元求解异或方程组

    第一道高斯消元题目~ 题目:有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为开就变为关 ...

  2. Bzoj4481 [Jsoi2015]非诚勿扰

    Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 147  Solved: 75 Description [故事背景] JYY赶上了互联网创业的大潮,为非 ...

  3. UOJ#204 【APIO2016】Boat

    Time Limit: 70 Sec  Memory Limit: 256 MBSubmit: 559  Solved: 248 Description 在首尔城中,汉江横贯东西.在汉江的北岸,从西向 ...

  4. Bzoj4710 [Jsoi2011]分特产

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 96  Solved: 62[Submit][Status][Discuss] Description ...

  5. 20155335俞昆《java程序设计》第10周总结

    学号 2016-2017-2 <Java程序设计>第十周学习总结 ## 事实上网络编程,我们可以简单的理解为两台计算机相互通讯数据而已,对于程序员而言,掌握一种编程接口并使用一种编程模型相 ...

  6. tornado简单使用

    这篇适用于快速上手想了解更深:http://www.tornadoweb.cn/   https://tornado-zh.readthedocs.io/zh/latest/ Tornado 是 Fr ...

  7. 简单的自动化运维工具(shell+except+whiptail+功能模块化函数+循环)

    简单的自动化运维工具(shell+except+whiptail+功能模块化函数+循环) http://www.cnblogs.com/M18-BlankBox/p/5881700.html

  8. pycaffe做识别时通道转换问题

    转自---------------------  作者:Peanut_范  来源:CSDN  原文:https://blog.csdn.net/u013841196/article/details/7 ...

  9. C++中delete和delete[]的区别(转)

    原文链接:http://www.cnblogs.com/charley_yang/archive/2010/12/08/1899982.html 一直对C++中的delete和delete[]的区别不 ...

  10. 六:ZooKeeper的java客户端api的使用

    一:客户端链接测试 package com.yeepay.sxf.createConnection; import java.io.IOException; import org.apache.zoo ...