# UVa 673 ### 題目連結:[UVa673](http://domen111.github.io/UVa-Easy-Viewer/?673) ### 題述: 在本題中,題目會先給你一個包含小括號()及中括號〔〕的字串。當字串符合下列條件時我們稱他為正確的運算式: 1. 該字串為一個空字串 2. 如果A和B都為正確的運算式,則AB也為正確的運算式, 3. 如果A為正確的運算式,則(A)及〔A〕都為正確的運算式。 現在,請你寫一支程式可以讀入這類字串並檢查它們是否為正確的運算式。字串的最大長度為 128 個字元。 --- 輸入的第一列為正整數 n ,代表接下來有 n 列待測資料。 --- 檢查每列待測資料,如果正確輸出 Yes ,否則輸出 No 。 ### c++ code: ```cpp= #include<iostream> #include<stdio.h> #include<string.h> #include<string> #include<stdlib.h> #include<stack> using namespace std ; int main () { int n ; //有幾組測資 cin >> n ; cin.get() ; int i ; int e ; string c ; stack<int> st ; while ( n ) { e = 0 ; getline ( cin , c ) ; if ( c == " " ) { printf ( "Yes\n" ) ; } else { for ( i = 0 ; i <= c.size() ; i++ ) { if ( c[i] == '(' ) { st.push( 1 ) ; } else if ( c[i] == '[' ) { st.push( 2 ) ; } else if ( c[i] == ')' ) { if ( st.empty() ) { e = 1 ; } else { if ( st.top() == 1 ) { st.pop() ; } else { e = 1 ; } } } else if ( c[i] == ']' ) { if ( st.empty() ) { e = 1 ; } else { if ( st.top() == 2 ) { st.pop() ; } else { e = 1 ; } } } } if ( st.empty() && e == 0 ) { printf ( "Yes\n" ) ; } else { printf ( "No\n" ) ; } while( !st.empty() ) { st.pop() ; } } n-- ; } } ```  :::success **``sample input``** 3 (\[]) ((\[()]))) (\[()\[]()])() ::: :::success **``sample output``** Yes No Yes ::: #### [返回首頁](https://hackmd.io/@fkleofk/APCS#673) ###### tags: `APCS選修` `C++` `UVa`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up