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 ...
随机推荐
- 使用Native API 创建进程
使用 Native API 创建进程 最近几个星期一直在研究这个题目.因为关于方面的资料比较多(可以看下面的参考文章),所以开始时以为很快就结束了.谁知道真正动起手来才发现有很多要考虑的地方,不过还好 ...
- 08-5-switch
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 2019-5-21-C#-在-构造函数添加-CallerMemberName-会怎样
title author date CreateTime categories C# 在 构造函数添加 CallerMemberName 会怎样 lindexi 2019-05-21 11:28:32 ...
- 乐观、悲观锁、redis分布式锁
悲观锁总是假设最坏的情况,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会阻塞直到它拿到锁(共享资源每次只给一个线程使用,其它线程阻塞,用完后再把资源转让给 ...
- Flink 1.9 实战:使用 SQL 读取 Kafka 并写入 MySQL
上周六在深圳分享了<Flink SQL 1.9.0 技术内幕和最佳实践>,会后许多小伙伴对最后演示环节的 Demo 代码非常感兴趣,迫不及待地想尝试下,所以写了这篇文章分享下这份代码.希望 ...
- 使用git命令从github上clone项目
首先创建本地仓库(实际上就是创建一个文件夹,放项目代码),然后cd进文件夹, 初始化空的git仓库 注意:这里不初始化也是可以clone的 然后git clone url(url表示项目网址) 然后就 ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--07--06CMDB测试Linux系统采集硬件数据的命令02
#settings.py """ Django settings for AutoCmdb project. Generated by 'django-admin sta ...
- ACM中Java使用注意事项
1. String 类用来存储字符串,可以用charAt方法来取出其中某一字节,计数从0开始, 而不是像C/C++那样使用 []访问是每个字符. 2. 在主类中 main 方法必须是 public s ...
- 记一次server 2008 R2的按装过程
项目上一直在用的dell务器在去年年底突然出现系统过期,导致c盘的东西全部丢失.我们用激活工具激活,还是没能找回丢失的东西. 为了装这个系统,跟同事一起折腾了好些次,最后发现安装服务器的时候有个磁盘阵 ...
- Python学习之--数字转人民币读法(解决问题的方法很重要)
效果图: 实现代码: money = float(input("Please input the money:"))cop = int(money)Num = ['零','壹',' ...