# TeX Quotes(UVA272) ## [程式繳交區](https://hackmd.io/@Renektonn/Sy2LaT6D1g/edit) ## 題目 [點我](https://onlinejudge.org/external/2/272.pdf) ## 解題網站 [UVA](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=208#google_vignette) [ZJ](https://zerojudge.tw/ShowProblem?problemid=c007) ## 題意 給定多行字串 當第一次遇到",輸出\`\` 當第二次遇到",輸出'' 不斷循環 當遇到非"的字元,直接輸出字元。 ## 演算法 ``` 1.定義布林變數f=1 2.讀取一行,放到str中,若無法讀取,則停止 2.1.掃過str,若出現"字元以及f=1,則輸出`,並將f設為0,若出現"字元以及f=0,則輸出'',並將f設為1,否則,輸出原字元 2.2.回到2 ``` ## 程式碼 ```cpp= #include <iostream> #include <string> using namespace std; int main() { bool f = 1; // 定義布林變數 f,初始值為 1 string str; // 持續讀取一行字串 while (getline(cin, str)) { for (int i = 0; i < str.length(); ++i) { if (str[i] == '"') { // 若出現 " 字元 if (f) { cout << "``"; // 若 f 為 1,輸出 `` f = 0; // 將 f 設為 0 } else { cout << "''"; // 若 f 為 0,輸出 '' f = 1; // 將 f 設為 1 } } else { cout << str[i]; // 否則輸出原字元 } } cout << endl; // 換行處理 } return 0; } ```
×
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