bnuoj 1057 函数(模拟)

http://www.bnuoj.com/bnuoj/problem_show.php?pid=1057

【题意】:给定x的值,带入f(x)求函数值

【题解】:注意第一个数的符号可能是'+',这里把我坑死了。。。

【code】:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <string.h>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 char str[100000],tstr[1000];
10 int stack[10000];
11 void init()
12 {
13     int i;
14     for(i=0;i<10000;i++)
15     {
16         stack[i]=1;
17     }
18 }
19 
20 int main()
21 {
22     int t;
23     scanf("%d",&t);
24     while(t--)
25     {
26         scanf("%s%s",str,tstr);
27         int x = 0;
28         int i=2;
29         if(tstr[2]=='-') i=3;
30         for(;tstr[i];i++)
31         {
32             x = 10*x+tstr[i]-'0';
33         }
34         if(tstr[2]=='-') x=-x;
35         init();
36         int len = strlen(str);
37         int cnt=0,s=0;
38         for(i=5;i<len;i++)
39         {
40             if(str[i]=='+'&&i==5)  //居然有前导+号,坑死了
41             {
42                 continue;
43             }
44             if(str[i]=='-'&&i==5)
45             {
46                 stack[cnt]*=-1;
47             }
48             else if(str[i]=='-')
49             {
50                 cnt++;
51                 stack[cnt]*=-1;
52             }
53             else if(str[i]=='+')
54             {
55                 cnt++;
56             }
57             else if(str[i]>='0'&&str[i]<='9')
58             {
59                 s=0;
60                 while(i<len&&str[i]>='0'&&str[i]<='9')
61                 {
62                     s = s*10+str[i]-'0';
63                     i++;
64                 }
65                 i--;
66                 stack[cnt]*=s;
67             }
68             else if(str[i]=='x')
69             {
70                 if(str[i+1]=='^')
71                 {
72                     i+=2;
73                     s=0;
74                     while(i<len&&str[i]>='0'&&str[i]<='9')
75                     {
76                         s = s*10+str[i]-'0';
77                         i++;
78                     }
79                     i--;
80                     stack[cnt]*=pow(x,s);
81                 }
82                 else
83                 {
84                     stack[cnt]*=x;
85                 }
86             }
87         }
88         int sum = 0;
89         for(i=0;i<=cnt;i++)
90         {
91             sum+=stack[i];
92         }
93         printf("f(x)=%d\n",sum);
94     }
95     return 0;
96 }

 

 

posted @ 2013-09-30 20:09  crazy_apple  阅读(277)  评论(0编辑  收藏  举报