您的位置:首页 > 其它

hdoj 1870 愚人节的礼物 【简单的栈应用】

2014-08-01 10:44 302 查看
题目大意: 不解释

这道题只要是知道栈的基本东西:比如说,LIFO。。。,

做这道题只是简单的运用一下,<stack>函数

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1870
代码:

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
	stack<char> s;
	char str[1005];
	while(scanf("%s", str) == 1){
		int len = strlen(str);
		int ans = 0, i;
		for(i = 0; i < len; i ++){
			if(str[i] == '(')
			s.push(str[i]);
			else if(str[i] == ')'&&s.top() == '(')
			s.pop();
			else
			break;
		}
		while(!s.empty()){
			s.pop();
			++ans;
		}
		printf("%d\n", ans);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: