gpt4 book ai didi

c - 为什么下面的代码不能正确扫描所有输入?

转载 作者:行者123 更新时间:2023-11-30 18:12:18 25 4
gpt4 key购买 nike

此代码是给定问题的解决方案: https://www.hackerearth.com/international-women-hackathon-2016/algorithm/jp-and-rotations/

问题是在我将 m 操作作为 R 1 ,L 2 ,L 1 给出一个低于示例输入中指定的其他操作之后,它不接受任何进一步的输入并直接将输出打印为 2 ,我无法获得我在这里犯了一个错误,请帮助我。

#include <stdio.h>
#include<malloc.h>
struct node
{
unsigned long int data ;
struct node *next,*prev;
};

int main()
{
int n ,m,a,count=0,i,j;
char rot;
scanf("%d %d ", &n ,&m);
struct node *head1=NULL,*rear1,*ptr,*temp,*head2=NULL,*rear2;
if(head1==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf("%ld ",&temp->data);
temp->next=NULL;
head1=temp;
ptr=head1;
head1->prev=NULL;
}

for(i=1;i<=n-1;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf("%ld ",&temp->data);

ptr->next=temp;
temp->prev=ptr;
ptr=temp;
temp->next=NULL;

}

rear1=ptr;
temp=NULL;
ptr=NULL;
fflush(stdout);

if(head2==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf("%ld ",&temp->data);
temp->next=NULL;
head2=temp;
ptr=head2;
}
for(i=1;i<=n-1;i++)
{

temp=(struct node*)malloc(sizeof(struct node));
scanf("%ld ",&temp->data);

ptr->next=temp;
ptr=temp;
ptr->next=NULL;

}

rear2=ptr;
ptr=NULL;temp=NULL;
fflush(stdout);

for(i=0;i<m;i++)
{
scanf("%c %d",&rot,&a);
fflush(stdout);

if(rot=='L')
{
ptr=head1;
for(j=0;j<a;j++)
{
temp=ptr->next;
rear1->next=ptr;
ptr->prev=rear1;
rear1=ptr;
head1=temp;
ptr=head1;
}

count++;
ptr=NULL;
temp=NULL;

if(head1->data==head2->data && rear1->data==rear2->data)
{
break;

}
}
else if(rot=='R')
{
temp=head1;
ptr=rear1->prev;

for(j=0;j<a;j++)
{
temp->prev=rear1;
rear1->prev->next=NULL;
rear1->next=temp;
head1=rear1;
temp=head1;
rear1=ptr;
ptr=rear1->prev;

}

count++;
temp=NULL;
ptr=NULL;

if(head1->data==head2->data && rear1->data==rear2->data)
{
break;

}

}
}
printf("%d",count);

return 0;
}

最佳答案

尝试删除 scanf 语句中格式说明符后面的空格。它不会给你正确的最终输出。但是,我认为这就是你需要知道的。

试试这个代码:

#include <stdio.h>
#include<malloc.h>
struct node
{
unsigned long int data;
struct node *next, *prev;
};
int main()
{
int n, m, a, count = 0, i, j;
char rot;
scanf("%d %d", &n, &m);
struct node *head1 = NULL, *rear1 = NULL, *ptr = NULL, *temp = NULL, *head2 = NULL, *rear2 = NULL;
if (head1 == NULL)
{
temp = (struct node*)malloc(sizeof(struct node));
scanf("%ld", &temp->data);
temp->next = NULL;
head1 = temp;
ptr = head1;
head1->prev = NULL;
}
for (i = 1; i <= n - 1; i++)
{
temp = (struct node*)malloc(sizeof(struct node));
scanf("%ld", &temp->data);
ptr->next = temp;
temp->prev = ptr;
ptr = temp;
temp->next = NULL;
}
ptr->next = head1;
head1->prev = ptr;
rear1 = ptr;
temp = NULL;
ptr = NULL;
fflush(stdout);
fflush(stdin);
if (head2 == NULL)
{
temp = (struct node*)malloc(sizeof(struct node));
scanf("%ld", &temp->data);
temp->next = NULL;
head2 = temp;
ptr = head2;
head2->prev = NULL;
}
for (i = 1; i <= n - 1; i++)
{

temp = (struct node*)malloc(sizeof(struct node));
scanf("%ld", &temp->data);

ptr->next = temp;
temp->prev = ptr;
ptr = temp;
ptr->next = NULL;
}
ptr->next = head2;
head2->prev = ptr;
rear2 = ptr;
ptr = NULL; temp = NULL;
fflush(stdout);
fflush(stdin);
for (i = 0; i<m; i++)
{
scanf("%c %d", &rot, &a);
fflush(stdout);
fflush(stdin);
if (rot == 'L')
{
ptr = head1;
for (j = 0; j<a; j++)
{
temp = ptr->next;
rear1->next = ptr;
ptr->prev = rear1;
rear1 = ptr;
head1 = temp;
ptr = head1;
}
count++;
ptr = NULL;
temp = NULL;
if (head1->data == head2->data && rear1->data == rear2->data)
{
break;
}
}
else if (rot == 'R')
{
temp = head1;
ptr = rear1->prev;
for (j = 0; j<a; j++)
{
temp->prev = rear1;
rear1->prev->next = NULL;
rear1->next = temp;
head1 = rear1;
temp = head1;
rear1 = ptr;
ptr = rear1->prev;
}
count++;
temp = NULL;
ptr = NULL;
if (head1->data == head2->data && rear1->data == rear2->data)
{
break;
}
}
}
printf("%d", count);
return 0;
}

关于c - 为什么下面的代码不能正确扫描所有输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38343113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com