其他分享
首页 > 其他分享> > 栈专题996

栈专题996

作者:互联网

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 char chr[256];
 4 int TOP = 0;
 5 int TTOOPP = 0;
 6 int top = 0;
 7 int main(){
 8     int i = 1;
 9     for(;;i++){
10         chr[i] = getchar();
11         if(chr[i] == '\n') break;
12         if(chr[i]!='('&&chr[i]!=')'&&chr[i]!='['&&chr[i] != ']') top++;
13         if(chr[i] == '(') TOP++;
14         if(chr[i] == ')') TOP--;
15         if(chr[i] == '[') TTOOPP++;
16         if(chr[i] == ']') TTOOPP--;
17     }
18     if(TOP == 0&&TTOOPP == 0&&top == 0) cout<<"OK"<<endl;
19     else cout<<"Wrong"<<endl;
20     return 0;
21 }
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 char chr[256];
 4 int main(){
 5     int n;
 6     char i;
 7     cin>>n;
 8     i = getchar();
 9     for(int j = 1;j<=n;j++){    
10     int i = 1;
11     int TOP = 0;
12     int TTOOPP = 0;
13     int top = 0;
14     int ToP = 0;
15     while(getchar()!='\n')
16     {
17         if(getchar() == '('){
18         if(top>0){
19         cout<<"NO"<<endl;
20         i = 0;
21         break;
22         }        
23         else TOP++;
24 }
25         if(getchar() == ')') TOP--;
26         if(getchar() == '['){
27         if(top>0||TOP>0){
28         cout<<"NO"<<endl;
29         i = 0;
30         break;
31         }
32         else TTOOPP++;
33     }
34         if(getchar() == ']') TTOOPP--;
35         if(getchar() == '<') top++;
36         if(getchar() == '>') top--;
37         if(getchar() == '{'){
38         if(top>0||TOP>0||TTOOPP>0){
39         cout<<"NO"<<endl;
40         i = 0;
41         break;
42         }
43         else ToP++;
44     }
45         if(getchar() == '}') ToP--;
46     }
47     if(TOP == 0&&TTOOPP == 0&&top == 0&&ToP == 0) cout<<"YES"<<endl;
48     else cout<<"NO"<<endl;
49 }
50     return 0;
51 }
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 stack <char> chr;    
  4 int n;    
  5 bool bl;
  6 int l;
  7 int main(void)
  8 {
  9     string C;
 10     cin >> n;
 11     getchar();
 12     while (n--) {
 13         cin>>C;
 14         while (!chr.empty()) chr.pop();
 15         bl = 1;
 16         l = C.length();
 17         for (int i = 0; i<l;i ++) {
 18             if (C[i] == '<') {
 19                 chr.push(C[i]);
 20             }
 21             else if (C[i] == '(') {
 22                 if (!chr.empty()) { 
 23                     if (chr.top() == '<') {
 24                         bl = 0;
 25                         break;
 26                     }
 27                 }
 28                 chr.push(C[i]);
 29             }
 30             else if (C[i] == '[') {
 31                 if (!chr.empty()) { 
 32                     if (chr.top() == '('||chr.top() == '<') {
 33                         bl = 0;
 34                         break;
 35                     }
 36                 } 
 37                 chr.push(C[i]); 
 38             }
 39             else if (C[i] == '{') {
 40                 if (!chr.empty()) { 
 41                     if (chr.top() == '['||chr.top() == '('||chr.top() == '<') {
 42                         bl = 0;
 43                         break;
 44                     }
 45                 } 
 46                 chr.push(C[i]); 
 47             }
 48             else if (C[i] == '>') {
 49                 if (!chr.empty()) {
 50                     if (chr.top() == '<') {
 51                         chr.pop();
 52                     }
 53                     else {
 54                         bl = 0;
 55                         break;
 56                     }
 57                 } 
 58                 else {
 59                     bl = 0;
 60                     break;
 61                 }
 62             }
 63             else if (C[i] == ')') {
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 stack<int> s;
 4 stack<char> chr;
 5 const int N = 5e4+10;
 6 char str[N];    
 7 int M = 0;
 8 bool F = 0;
 9 int chk(char a){
10     if ((a=='-')||(a=='+')) return 1;
11     if ((a=='*')||(a=='/')) return 2;
12     if (a=='^') return 3;
13     return 0;
14 }
15 void cale(stack<int> &s,stack<char> &chr){
16 int a,b;
17 char x;
18 b=s.top();s.pop();
19 a=s.top();s.pop();
20 x=chr.top();chr.pop();
21 if(x == '+') s.push(a+b);
22 if(x == '-') s.push(a-b);
23 if(x == '*') s.push(a*b);
24 if(x == '/') s.push(a/b);
25 if(x == '^') s.push(pow(a,b));
26 }
27 int main(void){
28     cin>>str+1;
29     int n = strlen(str+1);
30    for(int i = 1;i<=n+1;i++){
31        if(str[i]>='0'&&str[i]<='9'){
32            M=M*10+str[i]-'0';
33            F=1;
34        }
35        else{
36        if(F == 1){        
37        s.push(M);
38        F = 0;
39        M = 0;
40        }
41     if(str[i] == '(') chr.push(str[i]);
42     if(str[i] == ')'){
43     while(chr.top() != '(')
44     cale(s,chr);
45     chr.pop();
46     }
47     if(str[i] == '+'||str[i] == '-'||str[i] == '*'||str[i] == '/'||str[i] == '^'){
48     while(chr.empty() == 0&&chk(str[i])<=chk(chr.top())) cale(s,chr);
49     chr.push(str[i]);
50     }
51     if(str[i]=='\0') while(!chr.empty()) cale(s,chr);
52     }
53     }
54     cout<<s.top()<<endl;
55     return 0;
56     
57 }

 

 64                 if (!chr.empty()) {
 65                     if (chr.top() == '(') {
 66                         chr.pop();
 67                     }
 68                     else {
 69                         bl = 0;
 70                         break;
 71                     }
 72                 }
 73                 else {
 74                     bl = 0;
 75                     break;
 76                 }
 77                 
 78             }
 79             else if(C[i] == ']') { 
 80                 if (!chr.empty()) {
 81                     if (chr.top() == '[') {
 82                         chr.pop();
 83                     }
 84                     else {
 85                         bl = 0;
 86                         break;
 87                     }
 88                 }
 89                 else {
 90                     bl = 0;
 91                     break;
 92                 }
 93             }
 94             else if (C[i] == '}') { 
 95                 if (!chr.empty()) {
 96                     if (chr.top() == '{') {
 97                         chr.pop();
 98                     }
 99                     else {
100                         bl = 0;
101                         break;
102                     }
103                 }
104                 else {
105                     bl = 0;
106                     break;
107                 }
108             }
109         }
110         if(chr.empty() && bl) cout<< "YES"<<endl;
111         else cout<<"NO"<<endl;
112     }
113     return 0;
114 }

还有第四第五题的python码

 

1 s = input()
2 
3 p = s.replace('^','**')
4 q = p.replace('/','//')
5 
6 print(eval(q))
 1 s = input()
 2 
 3 p = s.replace('^','**')
 4 q = p.replace('/','//')
 5 # print(q)
 6 
 7 q = q[:-1]
 8 #print(q)
 9 
10 try:
11     res = eval(q)
12 except SyntaxError:
13     print('NO')
14 else:
15     print(eval(q))

 

标签:996,int,top,else,break,bl,chr,专题
来源: https://www.cnblogs.com/Eruption/p/15848667.html