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).
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).
1003X^5+234X^4-12X^3-2X^2+987X-1000
3 1003X^5+234X^4-12X^3-2X^2+987X-1000
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.
思路:分成四部分,X前的数,X后的数,X和^,+和-;
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cmath> #include<map> #include<stack> #include<queue> using namespace std; typedef long long LL; char s[1000]; int n; LL pp(LL y) { LL p=1; for(int i=1; i<=y; i++) p*=n; return p; } int main() { while(~scanf("%d%*c",&n)) { memset(s,0x00,sizeof(s)); gets(s); int len=strlen(s); LL s1=0,s2=0,sum=0; int m=0,p=0; for(int i=0; i<len; i++) { if(s[i]<='9'&&s[i]>='0'&&!m) { s1+=s[i]-'0'; s1*=10; } else if(s[i]<='9'&&s[i]>='0'&&m) { s2+=s[i]-'0'; s2*=10; m=2; } else if(s[i]=='X') m=1; if((s[i]=='+'||s[i]=='-'||i==len-1)&&i!=0) { if(m==1) s2=10; if(s1==0) s1=10; if(s[p]=='-') sum-=(s1/10)*pp(s2/10); else sum+=(s1/10)*pp(s2/10); p=i; s1=s2=m=0; } } printf("%lld\n",sum); } }