使用者工具

網站工具


codes

#include <iostream>
#include <stack>
using namespace std;

string change(int n) {
	stack<int> s;
	while(n>0)
	{
		s.push(n%2);
		n/=2;
	}
	string m="";
//	while(!s.empty())
//	{
//		m+=to_string(s.top());
//		s.pop();
//	}
	for(int i=s.size(); i>0; --i)
	{
		m += to_string(s.top());
		s.pop();
	}
	return m;
}

int main()
{
	int num;
	cin >> num;
	string s=change(num);
	cout << s;
}

栈的使用[](){}

#include <iostream>
#include <stack>
using namespace std;

char change(char n)
{
	if(n==')') return '(';
	if(n==']') return '[';
	if(n=='}') return '{';
}

int main()
{
	stack<char> s;
	string p;
	cin >> p;
	for(int i=0; i<p.size(); ++i)
	{
		if(s.empty())
		{
			s.push(p[i]);
			continue;
		}
		else if(change(p[i]) == s.top()) {
			s.pop();
		}
		else {
			s.push(p[i]);
		}
	}
	
	if(s.empty()) printf("Yes");
	else printf("No");
}

队列

#include <iostream>
#include <queue>
using namespace std;

int main()
{
	int n, m;
	cin >> n >> m;
	
	queue<int> q;
	
	for(int i=1; i<=n; ++i)
	{
		q.push(i);
	}
	
	while(!q.empty())
	{
		for(int i=1; i<m; ++i)
		{
			q.push(q.front());
			q.pop();
		}
		cout << q.front() << " ";
		q.pop();
	}
	cout << endl;
}
codes.txt · 上一次變更: benojan