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

标签 c

此代码是给定问题的解决方案: 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/

相关文章:

c - 如何通过调用 exec 函数族的成员来获取程序运行的返回值?

c - 其他C文件中声明和定义的划分

c - libav - 对 'av_frame_alloc' 等的 undefined reference

c - 关于C中判断用户是否是文件所有者

c - 在 C 中将文件内容打印到标准输出低级 I/O

c - select()语句中的 "fd_set *writefds"参数用来做什么

c - linux下c中声明大小为2字节的字符

c++ - 如何在 ffmpeg 中使用硬件加速

c - 找出循环序列中的旋转次数

c - 如何从参数行获取数字?