题目描述
在 TeX 中,左双引号是 "``",右双引号是 "''"。 蒜头君输入一篇包含双引号的文章,你的任务是把它转换成 TeX 的格式。
输入格式 蒜头君的文章,字符数量不超过 1000。(注意文章可能有多行) 输出格式 转换后的文章。
输入样例
"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!''题解思路
1.自己的做法:不断读入c字符,每读到一个“"”就转换flag的状态,相当于在“``”“''”之间转换输出 2.书上的做法:定义一个字符数组,通过异或来实现状态的改变,理论上来讲确实更优!
谨记:可以通过异或^1来实现状态0和1之间的转换 新知识:之前一直以为getchar只能用char类型来接收,书中给出的代码表明int也可以!
代码
cpp
//自己的做法
#include <bits/stdc++.h>
using namespace std;
int main()
{
char c;
bool flag = 0;
while((c = getchar())!= EOF)
{
if(c != '"') cout << c;
else
{
if(flag == 0)
{
cout << "``";
flag = 1;
}
else
{
cout << "''";
flag = 0;
}
}
}
return 0;
}cpp
//书上的代码
#include <cstdio>
int main()
{
int c, first = 1;
char s[2][4] = {"''","``"};
while((c = getchar()) != EOF)
{
if(c == '"') printf("%s", s[first]), first ^= 1;
else printf("%c", c);
}
return 0;
}
