[Round B] China New Grad Test 2014: Problem E. Ignore all my comments
2013-10-15 22:22
477 查看
【转载请注明出处: http://blog.csdn.net/lzl124631x】
OJ: https://code.google.com/codejam/contest/2929486/dashboard#s=p4
思路:
按行为单位输入.
设置level标志指示当前在注释的第几层中, 初始为0.
普通文本状态(level == 0)下正常读入, 当遇到"/*"时level++.
注释状态(level > 0)下跳过输入, 当遇到"/*"时level++, 遇到"*/"时level--.
仅在level == 0的行尾输出'\n'.
代码:
OJ: https://code.google.com/codejam/contest/2929486/dashboard#s=p4
思路:
按行为单位输入.
设置level标志指示当前在注释的第几层中, 初始为0.
普通文本状态(level == 0)下正常读入, 当遇到"/*"时level++.
注释状态(level > 0)下跳过输入, 当遇到"/*"时level++, 遇到"*/"时level--.
仅在level == 0的行尾输出'\n'.
代码:
#include <stdio.h> #define MAX_SIZE 100000 #define COMMENT_START(s) (*(s) == '/' && *((s) + 1) == '*') #define COMMENT_END(s) (*(s) == '*' && *((s) + 1) == '/') int main(){ char str_in[MAX_SIZE] = {0}; char str_out[MAX_SIZE] = {0}; char *p_in, *p_out; int level; freopen("E-large-practice.in","r",stdin); freopen("out.txt","w",stdout); level = 0; p_out = str_out; while(gets(str_in)){ p_in = str_in;// new line while(*p_in){ if(level == 0){// text mode while(*p_in && !COMMENT_START(p_in)){ *p_out++ = *p_in++; } if(*p_in){ p_in += 2; level++; } }else{// comment mode while(*p_in && !COMMENT_START(p_in) && !COMMENT_END(p_in)) p_in++; if(COMMENT_START(p_in)){ p_in += 2; level++; }else if(COMMENT_END(p_in)){ p_in += 2; level--; } } } if(level == 0) *p_out++ = '\n';// output '\n' at end of line in text mode } *p_out = 0; printf("Case #1:\n%s", str_out); return 0; }
相关文章推荐
- Google中国2014校园招聘笔试Round B China New Grad Test Problem E. Ignore all my comments
- Google China New Grad Test 2014 Round A Problem A
- Google China New Grad Test 2014 Round A Problem B
- Problem C. Sorting---Round A China New Grad Test 2014
- Google China New Grad Test 2014 Round A Problem C
- Google China New Grad Test 2014 Round A Problem D
- Google中国2014校园招聘笔试Round B China New Grad Test Problem B. Meet and party
- Round A China New Grad Test 2014 Problem A. Read Phone Number
- Google China New Grad Test 2014 Round A Problem E
- Round A China New Grad Test 2014---很基本的一道题Problem B. Rational Number Tree
- Round A China New Grad Test 2014 Problem C. Sorting
- Google中国2014校园招聘笔试Round B China New Grad Test Problem C. Hex
- Google中国2014校园招聘笔试Round A China New Grad Test Problem E. Spaceship Defence
- Google中国2014校园招聘笔试Round A China New Grad Test Problem A. Read Phone Number
- [Round B] China New Grad Test 2014: Problem A. Sudoku Checker
- Google中国2014校园招聘笔试Round A China New Grad Test Problem B. Rational Number Tree
- Google中国2014校园招聘笔试Round A China New Grad Test Problem D. Cross the maze
- Google中国2014校园招聘笔试Round B China New Grad Test Problem D. Dragon Maze
- Captain Hammer - Practice Round China New Grad Test 2014 - 精度控制
- Practice Round China New Grad Test 2014