2017SDUT期末机试——素数链表

来源:互联网 发布:淘宝音乐 编辑:程序博客网 时间:2024/06/06 08:37
Time Limit: 1000MS Memory Limit: 65536KB
    

Problem Description

我们定义素数链表为元素全部是素数的链表。

给定一个初始含有 n 个元素的链表,并给出 q 次删除操作,对于每次操作,你需要判断链表中指定位置上的元素,如果元素存在且不是素数则删除。

在所有操作完成后你还需要检查一下最终链表是否是一个素数链表。

Input

输入数据有多组。第 1 行输入 1 个整数 T (1 <= T <= 25) 表示数据组数。

对于每组数据:

  • 第 1 行输入 2 个整数 n (1 <= n <= 50000), q (1 <= q <= 1000) 表示链表初始元素数量和操作次数
  • 第 2 行输入 n 个用空格隔开的整数(范围 [0, 1000])表示初始链表
  • 接下来 q 行,每行输入 1 个整数 i (1 <= i <= 50000),表示试图删除链表中第 i 个元素

Output

对于每组数据:

  • 先输出 1 行 "#c",其中 c 表示当前是第几组数据
  • 对于每次删除操作,根据情况输出 1 行:
    • 如果要删除的位置不存在元素(位置超出链表长度),则输出 "Invalid Operation"
    • 如果要删除的位置存在元素且此位置的元素是非素数,则删除元素并输出 "Deleted x",其中 x 为成功删除的数(必须为非素数才能删除)
    • 如果要删除的位置存在元素且此位置的元素是素数,则输出 "Failed to delete x",其中 x 为此位置上的数
  • 删除操作全部进行完毕后,则还需判断该链表现在是否为一个素数链表。如果链表非空且是素数链表,则输出 "All Completed. It's a Prime Linked List",否则输出 "All Completed. It's not a Prime Linked List"

所有输出均不包括引号。

Example Input

21 20516 31 2 3 3 4 5114

Example Output

#1Invalid OperationDeleted 0All Completed. It's not a Prime Linked List#2Deleted 1Failed to delete 2Deleted 4All Completed. It's a Prime Linked List

Hint

推荐直接复制粘贴输出语句字符串到你的代码中,以防手打敲错。

链表中第 1 个元素的位置为 1,第 2 个元素的位置为 2,以此类推。

Author

注意把握大方向,大方向只要对了wa时多注意小细节,代码不要转换大方向
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>typedef struct node{    int data;    struct node*next;}str;str*creat(str*head,int n){    int i;    str*p,*tail;    head->next=NULL;    p=head->next;    tail=head;    for(i=0;i<n;i++)    {        p=(str*)malloc(sizeof(str));        scanf("%d",&p->data);        tail->next=p;p->next=NULL;        tail=p;    }    return head;}int pan(int x){    int i;    if(x==0||x==1)    {        return 0;    }    else    for(i=2;i<=sqrt(x);i++)//判断素数    {        if(x%i==0)        {            return 0;        }    }        return 1;}void delate(str*head,int x){    str*p,*tail;    tail=head;    p=head->next;    x=x-1;   while(x--)   {       tail=p;       p=p->next;       if(p==NULL)       {           printf("Invalid Operation\n");           return ;       }   }   if(pan(p->data))   {       printf("Failed to delete %d\n",p->data);   }   else   {       printf("Deleted %d\n",p->data);       if(p==head->next)       {           head->next=p->next;           free(p);           return ;       }       else       {           tail->next=p->next;           free(p);           return ;       }   }}int main(){    int T,tt=1;    str*p,*head;    while(scanf("%d",&T)!=EOF)    {        int t=1,x,n,m;        while(t<=T)        {            tt=1;            scanf("%d%d",&n,&m);            head=(str*)malloc(sizeof(str));            head=creat(head,n);            printf("#%d\n",t);            t++;            p=head;            while(m--)            {                scanf("%d",&x);                delate(head,x);            }            if(p->next==NULL)            {                printf("All Completed. It's not a Prime Linked List\n");            }            else            {while(p->next!=NULL)            {                if(pan(p->next->data)==0)//注意设p为head,否则head->next会跳过判断                {                    tt=0;                    break;                }                p=p->next;            }            if(tt==0)            {                printf("All Completed. It's not a Prime Linked List\n");            }            else{printf("All Completed. It's a Prime Linked List\n");}            }        }    }    return 0;}

0 0
原创粉丝点击