1038 约瑟夫环 循环单链表模拟

#include <cstdio>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <cstdlib>
#include <stdio.h>
#include<algorithm>
using namespace std;
int m = 0;
struct node
{
	int data;
	node* next;
};

node* head = new node;

void movedelete(int &dx, node* &nowpos)
{
	for (int i = 1; i <= dx - 1; i++)
	{
		nowpos = nowpos->next;
	}
	//cout<<"delete "<<nowpos->next->data<<endl;
	nowpos->next = nowpos->next->next;
	nowpos = nowpos->next;
	//cout<<"nowpos "<<nowpos->data<<endl;
}

int main()
{

	cin >> m;
	node* p = head;
	for (int i = 1; i <= m; i++)
	{
		p->next = new node;
		p = p->next;
		p->data = i;
	}
	p->next = head->next;
	int nowm = m;
	node* nowpos = head->next;
	for (int i = 1; i <= m - 1; i++)
	{
		int ki = 0;
		scanf("%d", &ki);
		ki--;
		int dx = ki%nowm;
		if (dx == 0) dx = nowm;
		movedelete(dx, nowpos);
		nowm--;
	}
	cout << nowpos->data;


	return 0;
}

  http://acm.sjtu.edu.cn/OnlineJudge/problem/1050

    原文作者:Chips丶
    原文地址: https://www.cnblogs.com/Chips/p/5422823.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞