PTA 7-2 绗﹀彿閰嶅

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PTA 7-2 绗﹀彿閰嶅相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/realloc' title='realloc'>realloc   style   else   turn   stat   ||   閫氳繃   error   ring   

鐩存帴鐢ㄦ爤妯℃嫙鍗冲彲锛屾暟缁勫彲鍋氾紝浣嗗洜涓鸿繖鑺傛暟鎹粨鏋勬槸鏍堬紝涓轰簡鏈熸湯鑰冭瘯杩樻槸鎵嬪啓涓€涓嬫爤鐨勬搷浣滐紝鍊煎緱娉ㄦ剰鐨勬槸锛岃繖閬撻鐢╣ets鍑芥暟鍦≒TA涓婁細缂栬瘧閿欒锛岀敤scanf("%[^ ]", str)浼氭湁涓€涓牱渚嬫棤娉曢€氳繃锛屾渶鍚庢垜浣跨敤浜唖tring璇诲叆鏁版嵁锛屽簲璇ユ槸鎴戝scanf鏍煎紡鍖栬鍏ヤ笉澶熶簡瑙o紝鏈夌煡閬撶殑鏈嬪弸鍙互璇勮鍖哄憡璇夋垜锛岄潪甯告劅璋?/p>

闄勪笂浠g爜锛?/p>

  1 #include <stdio.h>
  2 #include <malloc.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 
  6 #include <iostream>
  7 #include <string>
  8 
  9 using namespace std;
 10 
 11 //鍑芥暟鐘舵€佺爜瀹氫箟
 12 #define TRUE        1
 13 #define FALSE       0
 14 #define OK          1
 15 #define ERROR       0
 16 #define INFEASIBLE -1
 17 #define OVERFLOW   -2
 18 
 19 typedef int Status;
 20 typedef char SElemType;
 21 
 22 #define STACK_INIT_SIZE 500
 23 #define STACKINCREMENT 10
 24 
 25 typedef struct {
 26     SElemType *base;
 27     SElemType *top;
 28     int stacksize;
 29 }SqStack;
 30 
 31 Status InitStack(SqStack &S) {
 32     S.base = (SElemType * )malloc(STACK_INIT_SIZE * sizeof(SElemType));
 33     if(!S.base) exit(OVERFLOW);
 34     S.top = S.base;
 35     S.stacksize = STACK_INIT_SIZE;
 36     return OK;
 37 }//InitStack
 38 
 39 Status GetTop(SqStack &S, SElemType &e) {
 40     if(S.top == S.base)
 41         return ERROR;
 42     e = *(S.top - 1);
 43     return OK;
 44 }//GetTop
 45 
 46 Status Push(SqStack &S, SElemType e) {
 47     if(S.top - S.base >= S.stacksize) {
 48         S.base = (SElemType * )realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
 49         if(!S.base)
 50             exit(OVERFLOW);
 51         S.top = S.base + S.stacksize;
 52         S.stacksize += STACKINCREMENT;
 53     }
 54     *S.top++ = e;
 55     return OK;
 56 }//Push
 57 
 58 Status Pop(SqStack &S, SElemType &e) {
 59     if(S.top == S.base)
 60         return ERROR;
 61     e = *--S.top;
 62     return OK;
 63 }//Pop
 64 
 65 char change(char ch){
 66     if(ch == 鈥?/span>(鈥?/span>)
 67         return 鈥?/span>)鈥?/span>;
 68     if(ch == 鈥?/span>{鈥?/span>)
 69         return 鈥?/span>}鈥?/span>;
 70     if(ch == 鈥?/span>[鈥?/span>)
 71         return 鈥?/span>]鈥?/span>;
 72     if(ch == 鈥?/span>a鈥?/span>)
 73         return 鈥?/span>b鈥?/span>;
 74     if(ch == 鈥?/span>)鈥?/span>)
 75         return 鈥?/span>(鈥?/span>;
 76     if(ch == 鈥?/span>}鈥?/span>)
 77         return 鈥?/span>{鈥?/span>;
 78     if(ch == 鈥?/span>]鈥?/span>)
 79         return 鈥?/span>[鈥?/span>;
 80     if(ch == 鈥?/span>b鈥?/span>)
 81         return 鈥?/span>a鈥?/span>;
 82 };
 83 
 84 int main()
 85 {
 86     SqStack sta;
 87     InitStack(sta);
 88 
 89     char ch, ptr[200] = {0}, cnt = 0;
 90 
 91     char ans = 鈥?/span> 鈥?/span>; // ( -> 1, { -> 2, [ -> 3, /* -> 4
 92 
 93     string str;
 94 
 95     while(getline(cin, str)) {
 96         if(str == ".")
 97             break;
 98         for(int i = 0; i < str.size(); ++i) {
 99             if(str[i] == 鈥?/span>(鈥?/span> || str[i] == 鈥?/span>{鈥?/span> || str[i] == 鈥?/span>[鈥?/span> || str[i] == 鈥?/span>)鈥?/span> || str[i] == 鈥?/span>}鈥?/span> || str[i] == 鈥?/span>]鈥?/span>)
100                 ptr[cnt++] = str[i];
101             else if(str[i] == 鈥?/span>/鈥?/span> && str[i + 1] == 鈥?/span>*鈥?/span>)
102                 ptr[cnt++] = 鈥?/span>a鈥?/span>, ++i;
103             else if(str[i] == 鈥?/span>*鈥?/span> && str[i + 1] == 鈥?/span>/鈥?/span>)
104                 ptr[cnt++] = 鈥?/span>b鈥?/span>, ++i;
105         }
106     }
107 
108     for(int i = 0; i < cnt; ++i) {
109         if(ptr[i] == 鈥?/span>(鈥?/span> || ptr[i] == 鈥?/span>{鈥?/span> || ptr[i] == 鈥?/span>[鈥?/span> || ptr[i] == 鈥?/span>a鈥?/span>)
110             Push(sta, ptr[i]);
111         else {
112             if(GetTop(sta, ch) == OK) {
113                 if(ptr[i] == change(ch)) {
114                     Pop(sta, ch);
115                 }
116                 else {
117                     ans = ch;
118                     break;
119                 }
120             }
121             else {
122                 ans = ptr[i];
123                 break;
124             }
125         }
126     }
127 
128     if(ans == 鈥?/span> 鈥?/span> && GetTop(sta, ch) == OK) {
129         ans = ch;
130     }
131 
132     if(ans == 鈥?/span> 鈥?/span>)
133         printf("YES");
134     else {
135         printf("NO
");
136         if(ans == 鈥?/span>(鈥?/span> || ans == 鈥?/span>{鈥?/span> || ans == 鈥?/span>[鈥?/span>)
137             printf("%c-?",ans);
138         else if(ans == 鈥?/span>a鈥?/span>)
139             printf("/*-?");
140         else if(ans == 鈥?/span>)鈥?/span> || ans == 鈥?/span>}鈥?/span> || ans == 鈥?/span>]鈥?/span>)
141             printf("?-%c",ans);
142         else
143             printf("?-*/");
144     }
145 
146     return 0;
147 }

 

以上是关于PTA 7-2 绗﹀彿閰嶅的主要内容,如果未能解决你的问题,请参考以下文章

第03次作业-栈和队列

week3-栈和队列

week03-栈和队列

pta结构体链表作业

[code] PTA 胡凡算法笔记 DAY039

PTA 7-2 歌唱比赛计分