c007: 00272 - TeX Quotes

  1. 1. 內容
  2. 2. 範例輸入
  3. 3. 範例輸出
  4. 4. 想法
  5. 5. 程式碼

CPE 一顆星選集 49 道必考題

題目連結:https://zerojudge.tw/ShowProblem?problemid=c007

內容

給定若干列文字,將每組雙引號的第一個用``代替、第二個用’’代替。

範例輸入

“To be or not to be,” quoth the Bard, “that is the question”.
The programming contestant replied: “I must disagree.
To `C’ or not to `C’, that is The Question!”

範例輸出

``To be or not to be,’’ quoth the Bard, ``that is the question’’.
The programming contestant replied: ``I must disagree.
To `C’ or not to `C’, that is The Question!’’

想法

使用 getline 一次讀取一列,接著遍歷,遇到雙引號就判斷,其餘則直接輸出。

程式碼

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false), cin.tie(nullptr)
using namespace std;

int main(){
fastio;
string s;
bool dir = true;
while(getline(cin, s)){
for(int i = 0; i < s.length(); ++i){
if(s[i] == '"'){
if(dir){
cout << "``";
dir = !dir;
}
else {
cout << "''";
dir = !dir;
}
}
else cout << s[i];
}
cout << "\n";
}
}