← Back to list

UVa-00272-TeX Quotes

CPE 一星題

Zoey · 2025-08-09 21:33 · 0 claps · 1.5 min read
#cpe #cpe一星題 #uva
Open on Medium ↗

UVa-00272-TeX Quotes

CPE 一星題

英文題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=208

中文題目:https://zerojudge.tw/ShowProblem?problemid=c007

解題過程:

一開始有抓對方向,從 string 字串下手,並用 str.size() 或 str.length()判斷字串大小,接著使用for迴圈一一比對每個字元是否等於雙引號(記得兩兩一組)。

如果是且每組的第一個出現,就輸出「 ``」,反之輸出兩個單引號「 ''」。

如果不是,直接輸出字元本身。

實際操作時遇到幾個小問題,都是很基礎但我常常犯錯的部分。

  1. 比較運算子,在單引號、雙引號前要加上「 \ 」才能比較。
str[i] == '\"';
cout << "\'\'";
  1. 題目中輸入的雙引號兩個一組,因此是 % (mod) 2,而不是 % 4。(題目沒看清楚)

  2. 紀錄次數的變數 cnt 要放在while迴圈外面,如果放在while裡面,就會導致每讀一行 cnt 就重置,無法正確判斷。

#include<iostream>

using namespace std;

string str;

int main(){
    int cnt = 0; //放迴圈外面
    while(getline(cin , str)){

        for(int i = 0; i < str.size(); i++){
            if(str[i] == '\"'){
                cnt++;
                if((cnt % 2 == 1)){ //如果是每組的第一個
                    cout << "``";
                }else{
                    cout << "\'\'";
                }
            }else{
                cout << str[i];
            }
        }
        cout << "\n";
    }
}

메타데이터
post_id
6ec5c2ed6bf4
slug
uva-00272-tex-quotes-6ec5c2ed6bf4
url
https://medium.com/@wuzoey2020/uva-00272-tex-quotes-6ec5c2ed6bf4
canonical_url
https://medium.com/@wuzoey2020/uva-00272-tex-quotes-6ec5c2ed6bf4
author_url
https://medium.com/@wuzoey2020
status
ok
fetched_at
2026-06-24 23:31:39