Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 黑客删除好友:运行时错误-NZEC_Java_Linked List_Runtime Error_Puzzle - Fatal编程技术网

Java 黑客删除好友:运行时错误-NZEC

Java 黑客删除好友:运行时错误-NZEC,java,linked-list,runtime-error,puzzle,Java,Linked List,Runtime Error,Puzzle,我被这个问题困住了。我的代码通过了示例中给出的所有测试用例,但代码中存在一些错误。请指出错误 问题陈述() 获得博士学位后,克里斯蒂在她的大学里成了名人,她的facebook档案中充满了好友请求。作为一个好女孩,克里斯蒂接受了所有的要求 现在库尔德普嫉妒她从其他男人那里得到的所有关注,所以他要求她从朋友名单中删除一些男人。 为了避免“场景”,克里斯蒂决定从她的朋友列表中删除一些朋友,因为她知道每个朋友的人气,所以她使用以下算法删除一个朋友 算法删除(朋友): DeleteFriend=false

我被这个问题困住了。我的代码通过了示例中给出的所有测试用例,但代码中存在一些错误。请指出错误

问题陈述()

获得博士学位后,克里斯蒂在她的大学里成了名人,她的facebook档案中充满了好友请求。作为一个好女孩,克里斯蒂接受了所有的要求

现在库尔德普嫉妒她从其他男人那里得到的所有关注,所以他要求她从朋友名单中删除一些男人。 为了避免“场景”,克里斯蒂决定从她的朋友列表中删除一些朋友,因为她知道每个朋友的人气,所以她使用以下算法删除一个朋友

算法删除(朋友):

DeleteFriend=false
对于i=1到Friend.length-1
if(友人[i].人气<友人[i+1].人气)
删除我的朋友
DeleteFriend=true
打破
if(DeleteFriend==false)
删除最后一个朋友
输入: 第一行包含T个测试用例。每个测试用例的第一行包含N,Christie当前拥有的好友数和K,Christie决定删除的好友数。接下来的几行是她的朋友们的人气,他们之间用空格隔开

输出: 对于每个测试用例,在删除K个好友后,打印N-K数字,表示克里斯蒂好友的受欢迎程度

注 删除K个好友后的好友顺序应保持为输入中给出的顺序

我的解决方案

    class TestClass {
static class Node
{
    int data;
    Node next;
    Node(int d)
    {
        data = d;
        next = null;
    }}
static Node head = null;
public static void main(String args[] ) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String line = br.readLine();
    int cases = Integer.parseInt(line);
    for (int i = 0; i < cases; i++) {
        line = br.readLine();
        int friends = Integer.parseInt(line);
        line = br.readLine();
        int delete = Integer.parseInt(line);
        head = null;
        Node p =null;
        for(int j=0;j < friends;j++){
            line = br.readLine();
            int temp = Integer.parseInt(line);
            if(head == null){
                head = new Node(temp);
                p = head;
            }
            else{
                Node q = new Node(temp);
                p.next = q;
                p = q;
            }}
        delete_friend(head , delete);
        print_list(head);
    }}
static void delete_friend(Node h, int delete){
    Node p = head;
    Node q = null;
    int flag = 0;
    for (int x = 1; x<=delete;x++){
        p = head;
        flag = 0;
        q = p.next;
        while(p.next != null){
            q = p.next;
            if(p.data < q.data){
                p.data = q.data;
                p.next = q.next;
                flag=1;
                p = head;
                break;
            }
            if (flag == 0 && q.next == null){
                if (p.data >= q.data) {
                    p.next = null;
                    break;
                }}
            p = p.next;
        }}}
static void print_list(Node head){
    Node tnode = head;
    while (tnode != null)
    {
        System.out.print(tnode.data+" ");
        tnode = tnode.next;
    }
    System.out.println();
}}
class测试类{
静态类节点
{
int数据;
节点下一步;
节点(int d)
{
数据=d;
next=null;
}}
静态节点头=空;
公共静态void main(字符串args[])引发异常{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
String line=br.readLine();
int cases=Integer.parseInt(行);
对于(int i=0;i
如果你能分享你所遇到的错误,那会有所帮助。
您的
br.readLine()
正在读取包含好友数和要删除的好友数的完整行,并对其进行解析以获得正确的好友数将给出错误信息。尝试使用扫描仪或
br.read()
一次读取一个输入,看看这是否解决了您的问题。

我正在获取解决此问题的TLE。我不知道为什么

#包括
#包括
结构节点
{
国际知名度;
结构节点*链接;
}*start=NULL;
void delete(结构节点**开始,int d)
{
结构节点*当前=*开始;
结构节点*max=*start;
结构节点*temp;
while(当前!=NULL&¤t->link!=NULL&&d)
{
如果(当前->链接->人气<最大->人气)
{
温度=当前->链接;
当前->链接=临时->链接;
免费(临时);
d--;
}
其他的
{
当前=当前->链接;
最大值=电流;
}
}
}
结构节点*反向(结构节点*根)
{
结构节点*temp;
如果(根==NULL | |根->链接==NULL)
返回根;
温度=反向(根->链接);
根->链接->链接=根;
root->link=NULL;
返回温度;
}
int main()
{
结构节点*tmp,*p;
int t,n,i,item,k,d;
scanf(“%d”、&t);
而(t--)
{
p=start=NULL;
scanf(“%d”和“&n”);
scanf(“%d”、&d);
对于(i=0;i普及率=项目;
tmp->link=NULL;
if(start==NULL)
启动=tmp;
其他的
{
p=启动;
while(p->link)
p=p->link;
p->link=tmp;
}
}
开始=反向(开始);
删除(&start,d);
开始=反向(开始);
p=启动;
while(p!=NULL)
{
printf(“%d”,p->popularity);
p=p->link;
}
printf(“\n”);
}
返回0;
}

读取输入数据的方式有缺陷: 您的实现假设每行一个整数, 但这与问题描述不符:

每个测试用例的第一行包含N,Christie当前拥有的好友数和K,Christie决定删除的好友数。下一行包含Christie好友的人气,用空格隔开

不使用
缓冲读取器
, 我建议尝试使用
扫描仪
,这样更简单, 大概是这样的:

Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();

for (int i = 0; i < t; i++) {
    int friendsNum = scanner.nextInt();
    int toDeleteNum = scanner.nextInt();

    // ...

    for (int j = 0; j < friendsNum; j++) {
        int current = scanner.nextInt();

        // ...
    }

    // ...
}
Scanner Scanner=新的扫描仪(System.in);
int t=scanner.nextInt();
for(int i=0;i
修复输入解析后,一些测试将通过

但他们中的许多人仍将继续工作
#include <stdio.h>
#include <stdlib.h>
struct node
{
    int popularity;
    struct node *link;
}*start=NULL;

void delete(struct node **start,int d)
{

     struct node *current = *start;
     struct node *max = *start;
     struct node *temp;

     while (current != NULL && current->link != NULL && d)
     {
         if(current->link->popularity < max->popularity)
         {
             temp = current->link;
             current->link = temp->link;
             free(temp);
             d--;
         }
          else
         {
             current = current->link;
             max = current;
         }
     }

}

struct node* reverse(struct node *root)
{
    struct node *temp;  
    if(root==NULL || root->link==NULL)
        return root;
    temp=reverse(root->link);
    root->link->link=root;
    root->link=NULL;
    return temp;
}

int main()
{
    struct node *tmp,*p;
    int t,n,i,item,k,d;
    scanf("%d",&t);
    while(t--)
    {
        p=start=NULL;
        scanf("%d",&n);
        scanf("%d",&d);
        for(i=0;i<n;i++)
        {
            scanf("%d",&item);
            tmp=(struct node *)malloc(sizeof(struct node));
            tmp->popularity=item;
            tmp->link=NULL;
            if(start==NULL)
                start=tmp;
            else
            {
                p=start;
                while(p->link)
                    p=p->link;
                p->link=tmp;
            }
        }
        start=reverse(start);
        delete(&start,d);
        start=reverse(start);
        p=start;
        while(p!=NULL)
        {
            printf("%d ",p->popularity);
            p=p->link;
        }
        printf("\n");
    }

    return 0;
}
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();

for (int i = 0; i < t; i++) {
    int friendsNum = scanner.nextInt();
    int toDeleteNum = scanner.nextInt();

    // ...

    for (int j = 0; j < friendsNum; j++) {
        int current = scanner.nextInt();

        // ...
    }

    // ...
}
for (int j = 0; j < friendsNum; j++) {
    int current = scanner.nextInt();
    while (deleted < toDeleteNum && !stack.isEmpty() && stack.peek() < current) {
        stack.pop();
        deleted++;
    }
    stack.push(current);
}
while (deleted < toDeleteNum) {
    stack.pop();
    deleted++;
}