7-3 조세프 링 (25분)

1228 단어 PTA문제풀이
분명히 이것은 순환 체인 테이블이다. 이 학생에게 보고할 때 우리는 이 학생을 삭제하면 된다.본 蒟蒻의 코드가 좀 누추해서 아직 제때에 수정하지 못했으니 양해해 주십시오
#include 
using namespace std;
#include 
struct Node{

	int data;
	Node*next;
};

int main()
{
	int n, m;
	cin >> n >> m;

	vectorres;
	Node * first = new Node;
	//first->next =nullptr;
	Node *pre = new Node;
	Node * p = new Node;

	pre = first;
	pre->next = p;
	p = first;
	if (n){
		pre->data = 1;
	}
	for (int i = 2; i <= n; i++)
	{
		Node * pc = new Node;
		pc->data = i;
		pc->next = nullptr;
		p->next = pc;
		p = pc;
	}

	p->next = pre;

	//cout << "pre->data = " << pre->data <next = "<data<< endl;

	int cnt = 1;
	for (auto i = pre,j = p;n; i = i->next)
	{


		if (cnt == m)
		{
			auto pr = i;
			res.push_back(i->data);
			//cout << i->data << " ";
			j->next = i->next;
		//delete pr;
			n--;
			cnt = 0;
		}
		cnt++;
		j = i;
		//system("pause");
	}

	bool tf = false;
	for (auto &x : res)
	{
		if (tf)
			cout << " ";
		cout << x;
		tf = true;
	}

	system("pause");
	return 0;
}

좋은 웹페이지 즐겨찾기