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. 基于html5的动画库,非svg和canvas

    基于html5的动画库,非svg和canvas https://greensock.com/docs/#/HTML5/GSAP/TweenLite/

  2. 【BZOJ】2679: [Usaco2012 Open]Balanced Cow Subsets

    [算法]折半搜索+数学计数 [题意]给定n个数(n<=20),定义一种方案为选择若干个数,这些数可以分成两个和相等的集合(不同划分方式算一种),求方案数(数字不同即方案不同). [题解] 考虑直 ...

  3. 课下加分项目 MYPWD 20155335 俞昆

    Mypwd 的解读与实现 20155335 linux下pwd命令的编写 实验要求: 1 .学习pwd命令 2 . 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 .实现my ...

  4. 21、python操作redis的模块?

    什么是redis? redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...

  5. 9、MySQL常见的函数?

    请参考下面的博客文章: MySQL常见的函数

  6. 移动端测试===安卓设备共享程序-发布版本“share device”

    分享一个开源的项目 share device 项目地址:https://github.com/sunshine4me/ShareDevicePublish/tree/win7-x64 首先选择对应系统 ...

  7. 【bzoj4765】普通计算姬

    一道奇奇怪怪的数据结构题? 把树线性化,然后分块维护吧. 为了加速,求和用树状数组维护每个块的值. #include<bits/stdc++.h> #define N 100010 #de ...

  8. C++中多线程与Singleton的那些事儿

    前言 前段时间在网上看到了个的面试题,大概意思是如何在不使用锁和C++11的情况下,用C++实现线程安全的Singleton. 看到这个题目后,第一个想法就是用Scott Meyer在<Effe ...

  9. 使用while循环遍历文件

    /* 使用while循环遍历文件*/ [root@localhost test1]# vim 17.py //add #!/usr/bin/python ll = open('/tmp/1.txt') ...

  10. docker基于本地模版导入创建镜像

    /* 因为直接去网站拿会下载的慢,所以直接到网站里,对着此包--〉右键--〉复制链接地址 网站地址:https://openvz.org/Download/template/precreated */ ...