【題目敘述】https://zerojudge.tw/ShowProblem?problemid=c007
【解題想法】
- cnt: 紀錄「”」出現的次數
- 每次遇到「”」時,
- 若 cnt 為偶數:以「`」「`」取代
- 若 cnt 為奇數:以「’」「’」取代
#include <iostream>
using namespace std;
string s;
int cnt, blk;
int main() {
while (getline(cin, s)){
for (int i = 0; i < s.length(); i++){
if (s[i] == '\"'){
if (cnt % 2 == 0) cout << "``";
else cout << "''";
cnt++;
}
else cout << s[i];
}
cout << "\n";
}
}
Python code (credit: Amy Chou)
cnt = 0
while True:
try:
s = input()
for i in range(len(s)):
if s[i] == '"':
if cnt % 2 == 0:
print("``", end="")
else:
print("''", end="")
cnt += 1
else:
print(s[i], end="")
print()
except:
break