hdu 1296 Polynomial Problem(多项式模拟)
Problem Description
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.
自己写的超丑,查了一年的bug,最后发现是多组数据而我只写了一组.........哭了
#include <iostream>
#include <cstdio>
#include <string.h>
#include <math.h>
#include <algorithm>
#define MIN(x,y) ((x)>(y))?(y):(x)
#define MAX(x,y) ((x)>(y))?(x):(y) using namespace std; const int inf = 0x3f3f3f3f;
const double dinf = 0xffffffff;
const int vspot = ;
const int espot = ;
typedef long long ll; int bit[];
int x, weishu, cnt;
ll ans;
int bound;
bool e;
bool positive; long long getNum()
{
if( !weishu )
{
if(positive)
return ;
else
return -;
} ll num = ;
int k = weishu;
for( int i = ; i < k; i++ )
{
weishu--;
ll zhishu = ;
for( int j = ; j < weishu; j++ )
zhishu *= ;
num += (long long)(bit[i]*zhishu);
} if(positive)
return num;
else
return -num;
} bool check()
{
if ( cnt == bound )
return true;
return false;
} int main()
{ while( cin >> x )
{
string ads, str = "+";
cin >> ads; cnt = ;
e = false;
ans = ; if ( ads[]=='-' )
str = ads;
else
str += ads; bound = str.size() - ; while(true)
{
////////////////////符号部分////////////////////
if ( str[cnt++]=='+' )
positive = true;
else
positive = false; //////////////////////因数ai部分///////////////////
weishu = ;
memset( bit, -, sizeof(bit) );
while(true)
{
if ( str[cnt]>='' && str[cnt]<='' )
{
if ( check() )
{
e = true;
bit[weishu++] = str[cnt]-'';
ans += getNum();
break;
}
else
bit[weishu++] = str[cnt++]-'';
}
else
break;
}
if (e)
break;
//////////////X^x部分//////////////////////
ll ai = getNum();
int cishu = ;
if( check() )
e = true;
else
{
if ( str[cnt+] == '^' )
{
cnt++; cnt++;
if ( str[cnt]>='' && str[cnt]<='' )
cishu = str[cnt]-'';
else
{ cishu = ; cnt++;}
}
}
if ( check() )
e = true;
else
cnt++;
//////////////计算部分/////////////////////////
ll temp = ;
for( int i = ; i < cishu; i++ )
temp *= x;
ans += ai*temp;
if (e)
break;
}
cout << ans << endl;
}
return ;
}
再看看 别人写的.........我............我好菜呀QAQ
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int const nMax = ;
#define sf scanf
#define pf printf
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
char s[nMax];
int x;
#define ll long long
ll go(int &i)
{
int a,b,c;
a = ;
b = ;
if(s[i] == '-') a*=-,i++;
if(s[i] == '+') i++;
while(s[i]>='' && s[i]<='')
{
b = b* + s[i]-'';
i++;
}
if(b==) b = ;//这句就是应证X+1
c = ;
if(s[i] == 'X')
{
i++;
if(s[i]=='^')
{
i++; c = ;
while(s[i]>='' && s[i]<='')
{
c = c* + s[i] - '';
i ++;
}
}
else
{
c = ;
}
}
ll ret = ;
ret = (ll)a*b;
for(int i=; i<c; i++) ret *= x;
return ret;
}
int main()
{
while(cin >> x >> s)
{
int i = ;
int l = strlen(s);
ll ans = ;
while(i<l)
{
ans += go(i);
}
cout << ans << endl;
}
return ;
}
hdu 1296 Polynomial Problem(多项式模拟)的更多相关文章
- HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))
Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU 5867 Water problem (模拟)
Water problem 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5867 Description If the numbers ...
- HDOJ/HDU 1022 Train Problem I(模拟栈)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- HDU 1022 Train Problem I 模拟栈题解
火车进站,模拟一个栈的操作,额外的栈操作,查看能否依照规定顺序出栈. 数据量非常少,故此题目非常easyAC. 直接使用数组模拟就好. #include <stdio.h> const i ...
- HDU 5867 Water problem ——(模拟,水题)
我发这题只是想说明:有时候确实需要用水题来找找自信的~ 代码如下: #include <stdio.h> #include <algorithm> #include <s ...
- HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)
HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...
- HDU 3549 Flow Problem(最大流)
HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...
- hdu 5106 Bits Problem(数位dp)
题目链接:hdu 5106 Bits Problem 题目大意:给定n和r,要求算出[0,r)之间全部n-onebit数的和. 解题思路:数位dp,一个ct表示个数,dp表示和,然后就剩下普通的数位d ...
- HDU 3374 String Problem (KMP+最大最小表示)
HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others) Memory ...
随机推荐
- System.Web.Mvc.ViewResult.cs
ylbtech-System.Web.Mvc.ViewResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicK ...
- WebSocket实践
基本弄清楚了WebSocket的来龙去脉后,开始了实现WebSocket技术的探索. 看过一篇文章,测试了八种WebSocket框架性能,得以了解到实现WebSocket技术的框架有: Netty.U ...
- 转:进程上下文VS中断上下文
源地址:http://www.cnblogs.com/zzx1045917067/archive/2012/12/19/2824552.html 内核空间和用户空间是现代操作系统的两种工作模式,内核模 ...
- centos安装gcc4.8.2
1. 下载源码:镜像地址http://mirror.bjtu.edu.cn/gnu/gcc/gcc-4.8.2/gcc-4.8.2.tar.gz用svn下载可以随时更新到最新的版本svn checko ...
- javascript执行上下文和变量对象
执行上下文(execution context): 执行上下文就是当前 JavaScript 代码被解析和执行时所在环境的抽象概念. js语言是一段一段的顺序执行,这个“段”其实就是我们说的这个执行上 ...
- (转载)My97 datepicker使用指南
这里先显示大家都可以看到的My97DatePicker的用法: WdatePicker日历控件使用方法(http://www.cnblogs.com/yuhanzhong/archive/2011/0 ...
- 你所不知道的Mac截图的强大
Mac的截图功能扩展功能很强大的,不要用QQ那个COM+Ctrl+A弱爆了的截图了~ 首先说一下两种截图 1.Command+shift+3:全屏截图,保存截图到桌面 2.Command+shift+ ...
- Weekly Challenges - Week 11
一拖再拖,忍无可忍,自己懒的没救了. 一周前就结束的比赛,到现在才想起来补. 最后一题貌似又遇到了splay...还是不会!!!shit 题目链接 A题--快速幂 #include <cmath ...
- Eureka注册中心增加权限认证
在Spring Cloud通过Eureka实现服务注册与发现时,默认提供web管理界面,但是如果在生产环境暴露出来,会存在安全问题.为了解决这个问题,我们可以通过添加权限认证进行控制,具体步骤如下: ...
- 2019-1-29-dotnet-使用-Qpush-快速从电脑到手机推送文字
title author date CreateTime categories dotnet 使用 Qpush 快速从电脑到手机推送文字 lindexi 2019-01-29 16:22:56 +08 ...