试图解决Hackerrank中的挑战(从第M个元素到最后一个元素)

试图解决Hackerrank中的挑战(从第M个元素到最后一个元素),c,C,此处给出了问题的链接: 在C编程中,我基本上是想从链表的末尾找到第m个元素。我已经在我的IDE中实现了代码,它运行良好(需要一些微调),但在Hackerrank中不起作用。我无法解决这个问题。我可能认为在读取代码中的输入时存在问题。我得到的输出是“~stdout~没有响应”。我真的非常感谢任何关于如何克服这一点的帮助,因为我不熟悉如何解决hackerrank中的挑战。如果我遗漏了什么,请指出。 如果需要,请查看所附链接以了解问题陈述。 我在下面附上了我的代码供您参考。多谢各位 #include

此处给出了问题的链接:

在C编程中,我基本上是想从链表的末尾找到第m个元素。我已经在我的IDE中实现了代码,它运行良好(需要一些微调),但在Hackerrank中不起作用。我无法解决这个问题。我可能认为在读取代码中的输入时存在问题。我得到的输出是“~stdout~没有响应”。我真的非常感谢任何关于如何克服这一点的帮助,因为我不熟悉如何解决hackerrank中的挑战。如果我遗漏了什么,请指出。 如果需要,请查看所附链接以了解问题陈述。 我在下面附上了我的代码供您参考。多谢各位

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node *head;
int count;

struct node
{
    long int data;
    struct node* next;
    struct node* prev;
};

void Insert(long int val)
{
    struct node* temp = (struct node*) malloc(sizeof(struct node));
    if(count == 0)
    {
        temp->next = NULL;
        temp->prev = NULL;
        temp->data = val;
        head = temp;
    }
    else
    {
        struct node* temp1 = head;
        temp->next = temp1;
        temp->prev = NULL;
        temp->data = val;
        temp1->prev = temp;
        head = temp;
    }
    count++;
}

void Mthelement(long int M)
{
    long int i = 0;
    struct node* temp = head;
    while(temp->next != NULL)
    {
        temp = temp->next;
    }
    if (M == 1)
    {
        printf("%ld", temp->data);
    }
    else
    {
        while(i < (M-1))
        {
            if(temp->prev == NULL)
            {
                printf("NIL");
                return;
            }
            temp = temp->prev;
            i++;
        }
    printf("%ld", temp->data);
    }
}


int main() {
    head = NULL;
    long int M,L;
    printf("Enter value of M: ");
    scanf("%ld", &M);
    printf("Enter value of L: ");
    while(scanf("%ld", &L))
    {
        Insert(L);
    }
    Mthelement(M);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    return 0;
}
#包括
#包括
#包括
#包括
结构节点*头部;
整数计数;
结构节点
{
长整数数据;
结构节点*下一步;
结构节点*prev;
};
无效插入(长整型值)
{
结构节点*temp=(结构节点*)malloc(sizeof(结构节点));
如果(计数=0)
{
temp->next=NULL;
temp->prev=NULL;
温度->数据=val;
压头=温度;
}
其他的
{
结构节点*temp1=头部;
temp->next=temp1;
temp->prev=NULL;
温度->数据=val;
temp1->prev=温度;
压头=温度;
}
计数++;
}
无效设备(长整数米)
{
长整数i=0;
结构节点*温度=头部;
while(临时->下一步!=NULL)
{
温度=温度->下一步;
}
如果(M==1)
{
printf(“%ld”,临时->数据);
}
其他的
{
而(i<(M-1))
{
如果(临时->上一个==NULL)
{
printf(“无”);
返回;
}
温度=温度->上一个;
i++;
}
printf(“%ld”,临时->数据);
}
}
int main(){
head=NULL;
长整数M,L;
printf(“输入M的值:”);
scanf(“%ld”、&M);
printf(“输入L的值:”);
while(scanf(“%ld”、&L))
{
插入(L);
}
Mthelement(M);
/*在此处输入代码。从标准输入读取输入。将输出打印到标准输出*/
返回0;
}

我找到了问题的解决方案。似乎我所要做的就是像这样写代码
while(scanf(“%d”,&L)==1)以获取hackerrank中未知数量的输入。感谢大家抽出时间。

可能与您的输出已缓冲有关,但您从未打印换行符或手动刷新缓冲区。指向该问题的链接对我不起作用。你能解释一下任务,输入和输出应该是什么样子吗?一般来说,在这种挑战中,我们只是阅读输入,而不像
printf(“输入M的值:”)
但是如上所述,我们无法阅读确切的问题描述。只有两个建议:
void Insert(long int val)
可以改进,可能也有问题<代码>温度->数据=val可以移动到if-else之外,因为无论条件如何,您都可以执行相同的操作。在else的情况下,我会有类似的情况:
else{temp->next=NULL;temp->prev=head;head->next=temp;head=temp;}
我的问题是我无法编写一个从hackerrank驱动程序代码获得未知数量输入的代码。我遇到了困难,因为我不知道在使用while(1)循环的情况下何时停止获取输入,并且当我输入诸如say'\0'之类的字符时,没有指示是否必须停止。但我后来发现解决方案如下:while(scanf(“%d”,&L)==1{……。}。这是我当时面临的唯一问题,现在所有的测试用例都通过了。感谢大家花时间寻找解决方案。