用循环链表算不出答案。。
查看原帖
用循环链表算不出答案。。
498986
lianzhilu楼主2021/9/11 14:22

debug的时候Move函数里的for循环总是会循环多一次,找不出原因,求指教T_T

#include <iostream>
using namespace std;
struct toy
{
	int pos;
	char occ[10];
	struct toy *next;
};
struct toy *createList(int n)
{
	struct toy *head = NULL, *p1, *p2;
	p1 = p2 = new struct toy;
	cin >> p1->pos >> p1->occ;
	
	for(int i = 0; i < n-1; i++){
		if(!i)  head = p1;
		else  p2->next = p1;
		p2 = p1;
		p1 = new struct toy;
		cin >> p1->pos >> p1->occ;
	}
	
	p2->next = head;
	return head;
}
struct toy *Move(struct toy *head, int mov)
{
	struct toy *p0;
	p0 = head;
	for(int j = 0; j < mov; j++)
		p0 = p0->next;
	return p0;
}
struct toy *Find(struct toy *head, int a, int s, int n)
{
	struct toy *p1;
	p1 = head;
	if((p1->pos == 0 && a == 0) || (p1->pos == 1 && a == 1))
		p1 = Move(p1, n-s);
	else
		p1 = Move(p1, s);
	return p1;
}
int main()
{
	int n, m, a, s;
	cin >> n >> m;
	struct toy *p;
	p = createList(n);
	for(int i = 0; i < m; i++){
		cin >> a >> s;
		p = Find(p, a, s, n);
	}
	cout << p->occ;
}
2021/9/11 14:22
加载中...