bnuoj 29368 Check the Identity(栈)

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

【题解】:模拟,然后对x,进行枚举,看是否所有都满足条件

【code】:

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <math.h>
  4 #include <algorithm>
  5 #include <list>
  6 #include <string>
  7 #include <string.h>
  8 
  9 using namespace std;
 10 
 11 
 12 double stack[1000];
 13 int s_cnt;
 14 
 15 double fabs(double a)
 16 {
 17     return a>0?a:-a;
 18 }
 19 
 20 char op[1000];
 21 int len;
 22 
 23 int checkRight(double x)
 24 {
 25     int i;
 26     s_cnt = -1;
 27     for(i=0;i<len;i++)
 28     {
 29         char ch = op[i];
 30         if(ch=='x')
 31         {
 32             s_cnt++;
 33             stack[s_cnt] = x;
 34         }
 35         else if(ch=='s')
 36         {
 37             stack[s_cnt] = sin(stack[s_cnt]);
 38         }
 39         else if(ch=='c')
 40         {
 41             stack[s_cnt] = cos(stack[s_cnt]);
 42         }
 43         else if(ch=='t')
 44         {
 45             stack[s_cnt] = tan(stack[s_cnt]);
 46         }
 47         else if(ch=='-')
 48         {
 49             stack[s_cnt-1] = stack[s_cnt-1] - stack[s_cnt];
 50             s_cnt--;
 51         }
 52         else if(ch=='+')
 53         {
 54             stack[s_cnt-1] = stack[s_cnt]+stack[s_cnt-1];
 55             s_cnt--;
 56         }
 57         else if(ch=='*')
 58         {
 59             stack[s_cnt-1] = stack[s_cnt]*stack[s_cnt-1];
 60             s_cnt--;
 61         }
 62     }
 63     if(fabs(stack[0])>1e-8) return 0;
 64     return 1;
 65 }
 66 
 67 int main()
 68 {
 69     int t,cas=1;
 70     scanf("%d",&t);
 71     while(t--)
 72     {
 73         int n;
 74         scanf("%d",&n);
 75         int i;
 76         char str[10];
 77         s_cnt = -1;
 78         len = 0;
 79         for(i=0;i<n;i++)
 80         {
 81              scanf("%s",str);
 82              op[len] = str[0];
 83              len++;
 84         }
 85         double l=-2,r=2;
 86         double step = 4.0/1000;
 87         int flag=0;
 88         for(;l<=r;l++)
 89         {
 90             if(!checkRight(l))
 91             {
 92                 flag=1;
 93                 break;
 94             }
 95         }
 96         printf("Case %d: ",cas++);
 97         if(!flag)
 98         {
 99             puts("Yes");
100         }
101         else
102         {
103             puts("No");
104         }
105     }
106     return 0;
107 }
原文地址:https://www.cnblogs.com/crazyapple/p/3351481.html