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,最后发现是多组数据而我只写了一组.........哭了
1 #include <iostream> 2 #include <cstdio> 3 #include <string.h> 4 #include <math.h> 5 #include <algorithm> 6 #define MIN(x,y) ((x)>(y))?(y):(x) 7 #define MAX(x,y) ((x)>(y))?(x):(y) 8 9 using namespace std; 10 11 const int inf = 0x3f3f3f3f; 12 const double dinf = 0xffffffff; 13 const int vspot = 110; 14 const int espot = 10050; 15 typedef long long ll; 16 17 int bit[10500]; 18 int x, weishu, cnt; 19 ll ans; 20 int bound; 21 bool e; 22 bool positive; 23 24 long long getNum() 25 { 26 if( !weishu ) 27 { 28 if(positive) 29 return 1; 30 else 31 return -1; 32 } 33 34 ll num = 0; 35 int k = weishu; 36 for( int i = 0; i < k; i++ ) 37 { 38 weishu--; 39 ll zhishu = 1; 40 for( int j = 0; j < weishu; j++ ) 41 zhishu *= 10; 42 num += (long long)(bit[i]*zhishu); 43 } 44 45 if(positive) 46 return num; 47 else 48 return -num; 49 } 50 51 52 bool check() 53 { 54 if ( cnt == bound ) 55 return true; 56 return false; 57 } 58 59 60 int main() 61 { 62 63 while( cin >> x ) 64 { 65 string ads, str = "+"; 66 cin >> ads; 67 68 cnt = 0; 69 e = false; 70 ans = 0; 71 72 if ( ads[0]=='-' ) 73 str = ads; 74 else 75 str += ads; 76 77 bound = str.size() - 1; 78 79 while(true) 80 { 81 ////////////////////符号部分//////////////////// 82 if ( str[cnt++]=='+' ) 83 positive = true; 84 else 85 positive = false; 86 87 //////////////////////因数ai部分/////////////////// 88 weishu = 0; 89 memset( bit, -1, sizeof(bit) ); 90 while(true) 91 { 92 if ( str[cnt]>='0' && str[cnt]<='9' ) 93 { 94 if ( check() ) 95 { 96 e = true; 97 bit[weishu++] = str[cnt]-'0'; 98 ans += getNum(); 99 break; 100 } 101 else 102 bit[weishu++] = str[cnt++]-'0'; 103 } 104 else 105 break; 106 } 107 if (e) 108 break; 109 //////////////X^x部分////////////////////// 110 ll ai = getNum(); 111 int cishu = 1; 112 if( check() ) 113 e = true; 114 else 115 { 116 if ( str[cnt+1] == '^' ) 117 { 118 cnt++; cnt++; 119 if ( str[cnt]>='2' && str[cnt]<='9' ) 120 cishu = str[cnt]-'0'; 121 else 122 { cishu = 10; cnt++;} 123 } 124 } 125 if ( check() ) 126 e = true; 127 else 128 cnt++; 129 //////////////计算部分///////////////////////// 130 ll temp = 1; 131 for( int i = 0; i < cishu; i++ ) 132 temp *= x; 133 ans += ai*temp; 134 if (e) 135 break; 136 } 137 cout << ans << endl; 138 } 139 return 0; 140 }
再看看 别人写的.........我............我好菜呀QAQ
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int const nMax = 10100; 7 #define sf scanf 8 #define pf printf 9 #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) 10 char s[nMax]; 11 int x; 12 #define ll long long 13 ll go(int &i) 14 { 15 int a,b,c; 16 a = 1; 17 b = 0; 18 if(s[i] == '-') a*=-1,i++; 19 if(s[i] == '+') i++; 20 while(s[i]>='0' && s[i]<='9') 21 { 22 b = b*10 + s[i]-'0'; 23 i++; 24 } 25 if(b==0) b = 1;//这句就是应证X+1 26 c = 0; 27 if(s[i] == 'X') 28 { 29 i++; 30 if(s[i]=='^') 31 { 32 i++; c = 0; 33 while(s[i]>='0' && s[i]<='9') 34 { 35 c = c*10 + s[i] - '0'; 36 i ++; 37 } 38 } 39 else 40 { 41 c = 1; 42 } 43 } 44 ll ret = 0; 45 ret = (ll)a*b; 46 for(int i=0; i<c; i++) ret *= x; 47 return ret; 48 } 49 int main() 50 { 51 while(cin >> x >> s) 52 { 53 int i = 0; 54 int l = strlen(s); 55 ll ans = 0; 56 while(i<l) 57 { 58 ans += go(i); 59 } 60 cout << ans << endl; 61 } 62 return 0; 63 }