UVA 272: TEX Quotes

Jasonlin posted @ 2011年3月26日 10:05 in UVA , 2251 阅读

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=208

题目大意:

      TEX是计算机界圣人Donald Knuth的作品,写论文的必备工具!现在题目要你把一对双引号("")的第一个变成(``)第二个变成('').

解题思路:

      看下题目想得太简单,遇到第一个(")就输出(``)第二个就输出(''),结果错啦,数据不是全给(")而是和(``和'')混用的吧。弄一个标记,遇到一次这三种符号之一就把标记取反,标记为1输出“``”,为0输出“''”,标记要为整篇文章的,不能是一行的,因为每行是相连的。

解题代码:

#include<iostream>
#include<string>
using namespace std;
int main(){
	string s;
	int flag=1;
	while(getline(cin,s)){
		int len=s.length();
		for(int i=0;i<len;i++)
			if(s[i]=='"'){
				if(flag)
					cout<<"``",flag^=1;
				else
					cout<<"''",flag^=1;
			}
			else{
				if(i+1<len&&(s[i]=='`'&&s[i+1]=='`')||(s[i]=='\''&&s[i+1]=='\''))
						cout<<s[i]<<s[i+1],i++,flag^=1;
				else
				cout<<s[i];
			}
		cout<<endl;
	}
}

扩展知识:http://zzg34b.w3.c361.com/index.htm

  • 无匹配
  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter