M - 素数链表

本文介绍如何在给定链表中,根据操作指令删除非素数元素,并在删除完成后判断链表是否仍为素数链表。通过实例演示了输入处理、素数判断及链表操作的步骤。

M - 素数链表
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”
所有输出均不包括引号。

Sample
Input
2
1 2
0
5
1
6 3
1 2 3 3 4 5
1
1
4
Output
#1
Invalid Operation
Deleted 0
All Completed. It’s not a Prime Linked List
#2
Deleted 1
Failed to delete 2
Deleted 4
All Completed. It’s a Prime Linked List
Hint
推荐直接复制粘贴输出语句字符串到你的代码中,以防手打敲错。

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

#include <bits/stdc++.h>
using namespace std;
struct node 
{
    node *next;
    int data;
};
int n;                                       //表示当前链表所含元素的数目
bool prime(int a)                            //判断素数
{
    if (a == 0 || a == 1) return 0;
    int x = sqrt(a);
    for (int i = 2; i <= x; i++)
    {
        if (a % i == 0) return 0;
    }
    return 1;
}
node *creat(int n)                           //初始建立链表
 {
    int i;
    node *head, *tail, *p;
    head = new node;
    head->next = NULL;
    tail = head;
    for (i = 1; i <= n; i++)
    {
        p = new node;
        scanf("%d", &p->data);
        p->next = NULL;
        tail->next = p;
        tail = p;
    }
    return head;
}
node *dele(node *head, int a)                //删除操作
 {
    if (a > n)                               //位置超出链表长度
    {
        printf("Invalid Operation\n");
        return head;
    }
    a--;
    node *p, *q;
    q = head;
    p = head->next;
    while (a--)                              //找到想要删除的节点p
    {
        q = p;
        p = p->next;
    }
    if (prime(p->data) == 0)                //不是素数,删除成功
    {
        printf("Deleted %d\n", p->data);
        q->next = p->next;
        n--;
    } 
    else                                   //否则,删除失败
        printf("Failed to delete %d\n", p->data);
    return head;
}
bool f(node *head)                         //判断链表是否为一个素数链表
{
    if (!head->next) return 1;             //链表为空
    node *p;
    p = head->next;
    while (p) {
        if (prime(p->data) == 0)           //链表中有非素数
        return 1;
        p = p->next;
    }
    return 0;
}
int main() {
    int i, m, a, t;
    int cnt = 1;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%d", &n, &m);
        printf("#%d\n", cnt++);
        node *head;
        head = creat(n);
        while (m--)
        {
            scanf("%d", &a);
            head = dele(head, a);       //进行删除操作,并返回操作完的链表
        }
        if (f(head))
            printf("All Completed. It's not a Prime Linked List\n");
        else
            printf("All Completed. It's a Prime Linked List\n");
    }
}
解析: #define HASH_MAP_SIZE 123 typedef struct Entry Entry; struct Entry { AvahiHashmap *hashmap; void *key; void *value; AVAHI_LLIST_FIELDS(Entry, bucket); AVAHI_LLIST_FIELDS(Entry, entries); }; struct AvahiHashmap { AvahiHashFunc hash_func; AvahiEqualFunc equal_func; AvahiFreeFunc key_free_func, value_free_func; Entry *entries[HASH_MAP_SIZE]; AVAHI_LLIST_HEAD(Entry, entries_list); }; static Entry* entry_get(AvahiHashmap *m, const void *key) { unsigned idx; Entry *e; idx = m->hash_func(key) % HASH_MAP_SIZE; for (e = m->entries[idx]; e; e = e->bucket_next) if (m->equal_func(key, e->key)) return e; return NULL; } static void entry_free(AvahiHashmap *m, Entry *e, int stolen) { unsigned idx; assert(m); assert(e); idx = m->hash_func(e->key) % HASH_MAP_SIZE; AVAHI_LLIST_REMOVE(Entry, bucket, m->entries[idx], e); AVAHI_LLIST_REMOVE(Entry, entries, m->entries_list, e); if (m->key_free_func) m->key_free_func(e->key); if (m->value_free_func && !stolen) m->value_free_func(e->value); avahi_free(e); } AvahiHashmap* avahi_hashmap_new(AvahiHashFunc hash_func, AvahiEqualFunc equal_func, AvahiFreeFunc key_free_func, AvahiFreeFunc value_free_func) { AvahiHashmap *m; assert(hash_func); assert(equal_func); if (!(m = avahi_new0(AvahiHashmap, 1))) return NULL; m->hash_func = hash_func; m->equal_func = equal_func; m->key_free_func = key_free_func; m->value_free_func = value_free_func; AVAHI_LLIST_HEAD_INIT(Entry, m->entries_list); return m; } void avahi_hashmap_free(AvahiHashmap *m) { assert(m); while (m->entries_list) entry_free(m, m->entries_list, 0); avahi_free(m); } void* avahi_hashmap_lookup(AvahiHashmap *m, const void *key) { Entry *e; assert(m); if (!(e = entry_get(m, key))) return NULL; return e->value; } int avahi_hashmap_insert(AvahiHashmap *m, void *key, void *value) { unsigned idx; Entry *e; assert(m); if ((e = entry_get(m, key))) { if (m->key_free_func) m->key_free_func(key); if (m->value_free_func) m->value_free_func(value); return 1; } if (!(e = avahi_new(Entry, 1))) return -1; e->hashmap = m; e->key = key; e->value = value; AVAHI_LLIST_PREPEND(Entry, entries, m->entries_list, e); idx = m->hash_func(key) % HASH_MAP_SIZE; AVAHI_LLIST_PREPEND(Entry, bucket, m->entries[idx], e); return 0; } int avahi_hashmap_replace(AvahiHashmap *m, void *key, void *value) { unsigned idx; Entry *e; assert(m); if ((e = entry_get(m, key))) { if (m->key_free_func) m->key_free_func(e->key); if (m->value_free_func) m->value_free_func(e->value); e->key = key; e->value = value; return 1; } if (!(e = avahi_new(Entry, 1))) return -1; e->hashmap = m; e->key = key; e->value = value; AVAHI_LLIST_PREPEND(Entry, entries, m->entries_list, e); idx = m->hash_func(key) % HASH_MAP_SIZE; AVAHI_LLIST_PREPEND(Entry, bucket, m->entries[idx], e); return 0; } void avahi_hashmap_remove(AvahiHashmap *m, const void *key) { Entry *e; assert(m); if (!(e = entry_get(m, key))) return; entry_free(m, e, 0); } void avahi_hashmap_foreach(AvahiHashmap *m, AvahiHashmapForeachCallback callback, void *userdata) { Entry *e, *next; assert(m); assert(callback); for (e = m->entries_list; e; e = next) { next = e->entries_next; callback(e->key, e->value, userdata); } } unsigned avahi_string_hash(const void *data) { const char *p = data; unsigned hash = 0; assert(p); for (; *p; p++) hash = 31 * hash + *p; return hash; } int avahi_string_equal(const void *a, const void *b) { const char *p = a, *q = b; assert(p); assert(q); return strcmp(p, q) == 0; } unsigned avahi_int_hash(const void *data) { const int *i = data; assert(i); return (unsigned) *i; } int avahi_int_equal(const void *a, const void *b) { const int *_a = a, *_b = b; assert(_a); assert(_b); return *_a == *_b; }
最新发布
09-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值