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