新人求助一下,拜托各位大神了,我尝试用了数组和链表的写法,都是报错tle,我感觉自己思路也没错,有哪位大神能看出我这里哪里错了吗?
#include<iostream>
struct ring
{
bool face;
char job[100];
}ring[100000]{0,0},*p = ring;
int main()
{
int n,m;
std::cin >> n >> m;
for(int i = 0;i < n;i++)
scanf("%d %s",&ring[i].face,ring[i].job);
for(int i = 0;i < m;i++)
{
bool op;
int step;
std::cin >> op >> step;
if(op)
{
if(p ->face)
for(int j = 0;j < step;j++)
{
p--;
if(p == ring - 1)
p = ring + n - 1;
if(p == ring + n)
p = ring;
}
else
for(int j = 0;j < step;j++)
{
p++;
if(p == ring - 1)
p = ring + n - 1;
if(p == ring + n)
p = ring;
}
}
else
{
if(p ->face)
for(int j = 0;j < step;j++)
{
p++;
if(p == ring - 1)
p = ring + n - 1;
if(p == ring + n)
p = ring;
}
else
for(int j = 0;j < step;j++)
{
p--;
if(p == ring - 1)
p = ring + n - 1;
if(p == ring + n)
p = ring;
}
}
}
std::cout << p ->job;
}
以下是我用链表的方式改写的新程序,思路同上,报错tle同上。。
#include<iostream>
#include<string>
using namespace std;
struct node
{
struct node* last;
struct node* next;
bool face;
char job[10000];
};
struct node* makelist(bool face, char job[], struct node* tail)
{
struct node* p = (struct node*)malloc(sizeof(struct node));
p->face = face;
strcpy_s(p->job, job);
p->next = NULL;
if (tail == NULL)
{
p->last = NULL;
return p;
}
else
{
p->last = tail;
tail->next = p;
return p;
}
}
void free_list(struct node* head)
{
static struct node* beg = head;
if (head->next != beg)
{
struct node* p = head->next;
free(head);
free_list(p);
}
else
{
free(head);
return;
}
}
int main()
{
int n, m;
bool face;
char job[10000];
struct node* head{ NULL }, * tail{ NULL };
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> face >> job;
tail = makelist(face, job, tail);
if (i == 0)
head = tail;
}
head->last = tail;
tail->next = head;
for (int i = 0; i < m; i++)
{
cin >> face >> n;
if (face ^ head->face)
for (int j = 0; j < n; j++)
head = head->next;
else
for (int j = 0; j < n; j++)
head = head->last;
}
cout << head->job;
free_list(head);
}