问题描述
p272原题链接:UVaOJ 272 - TEX Quotes / POJ 1488
相关说明:本题为《算法竞赛入门经典(第2版)》例题 3-1
解法一:模拟
模拟读入,可一边读一边处理(逐字符或逐行地),关键是设置一个标志变量(此处为 start )判断引号类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <bits/stdc++.h> using namespace std; int main() { char ch; bool start = true; while ((ch = getchar()) != EOF) { if (ch == '"') { cout << (start ? "``" : "''"); start = !start; } else { cout << ch; } } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 |
import sys start = True for line in sys.stdin: for ch in line: if ch == "\"": print("``" if start else "''", end='') start = not start else: print(ch, end="") |