問題 1507: [藍橋杯][算法提高VIP]去註釋

題目鏈接:點擊這裏

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>

using namespace std;

int main()
{
	char c;
	char pre = 'x';
	bool dan = false, duo = false;
	
	while((c = cin.get()) != EOF)
	{
		if(dan)             //進入單行註釋 
		{
			if(c=='\n')     //遇到換行結束
			{
				cout<<endl;
				dan = false;
			}
			continue;
		}
		
		if(duo)             //進入多行註釋
		{
			if(pre == '*' && c == '/')  duo = false;    //遇到*/結束
			else    pre = c;    //此時需要記錄前一個字符作爲判斷
			continue;
		}
		
		if(c == '/')
		{
		    c = getchar();
			if(c == '/')    dan = true;
			else if(c == '*')   duo = true;
			else cout<<"/"<<c;
		}
		else
		    cout<<c;
	}
	
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章