小平的约会

时间: 1ms        内存:128M

描述:

 只需要提交补充的那一部分代码,提交时选择C/C++方式提交

小平买了n颗价值不同的珍珠,穿成了一串项链,准备送给gf。gf说只要珍珠的价值大于m,可以与小平约会。但是小乌龟伙同X偷走了最大的一颗,问剩余的珍珠价值是否能够达到女朋友的标准。

#include <stdio.h>
#include <stdlib.h>
struct necklace
{
    int value;
    struct necklace* next;
};
struct necklace *create_necklace(int n)
{
    struct necklace *head=NULL, *p, *last;  /* head是头指针,p指向新结点,last指向尾结点 */
    int value,i;
    for(i=0; i<n; i++)
    {
        scanf(“%d”, &value);
        p = (struct necklace *)malloc(sizeof(struct necklace));
        p->value = value;
        p->next = NULL;
        if (head==NULL)
            head = p;
        else
            last->next = p;
        last= p;
    }
    return head;
};

int check_necklace(int m,struct necklace *head)
{
    struct necklace *p;
    int sumvalue=0,maxvalue=0;
    p = head;
    /*
        完善并提交该部分代码
    */
}
void free_necklace(struct necklace *head)  /* 调用时,形式参数h得到链表的首结点 */
{
    struct necklace *p,*q;
    p = head;
    while(p!=NULL)
    {
        q=p;
        p = p->next;
        free(q);
    }
}
int main()
{
    struct necklace *head;
    int m,n;
    scanf(“%d%d”,&m,&n);
    head=create_necklace(n);
    if(check_necklace(m,head)==1)
        printf(“Yes”);
    else
        printf(“No”);
    free_necklace(head);
    return 0;
}

输入:

第一行,m,gf的标准
第二行,n(<80),表示珍珠的个数。
第三行,n个数,表示每个珍珠的价值

输出:

输出Yes或No

示例输入:

100
10
12 9 18 3 14 9 20 3 2 33 

示例输出:

No

提示:

参考答案(内存最优[1092]):


#include <stdio.h>
#include <stdlib.h>
struct necklace
{
    int value;
    struct necklace* next;
};
struct necklace *create_necklace(int n)
{
    struct necklace *head=NULL, *p, *last;  /* head是头指针,p指向新结点,last指向尾结点 */
    int value,i;
    for(i=0; i<n; i++)
    {
        scanf("%d", &value);
        p = (struct necklace *)malloc(sizeof(struct necklace));
        p->value = value;
        p->next = NULL;
        if (head==NULL)
            head = p;
        else
            last->next = p;
        last= p;
    }
    return head;
};

int check_necklace(int m,struct necklace *head)
{
    struct necklace *p;
    int sumvalue=0,maxvalue=0;
    p = head;
for(;p!=NULL;p=p->next)
     {
         if(p->value>maxvalue)
            maxvalue=p->value;
         sumvalue=sumvalue+p->value;
     }
     if(sumvalue-maxvalue>m)
        return 1;
     return 0;
}
void free_necklace(struct necklace *head)  /* 调用时,形式参数h得到链表的首结点 */
{
    struct necklace *p,*q;
    p = head;
    while(p!=NULL)
    {
        q=p;
        p = p->next;
        free(q);
    }
}
int main()
{
    struct necklace *head;
    int m,n;
    scanf("%d%d",&m,&n);
    head=create_necklace(n);
    if(check_necklace(m,head)==1)
        printf("Yes");
    else
        printf("No");
    free_necklace(head);
    return 0;
}

参考答案(时间最优[0]):


#include <stdio.h>
#include <stdlib.h>
struct necklace
{
    int value;
    struct necklace* next;
};
struct necklace *create_necklace(int n)
{
    struct necklace *head=NULL, *p, *last;  /* head是头指针,p指向新结点,last指向尾结点 */
    int value,i;
    for(i=0; i<n; i++)
    {
        scanf("%d", &value);
        p = (struct necklace *)malloc(sizeof(struct necklace));
        p->value = value;
        p->next = NULL;
        if (head==NULL)
            head = p;
        else
            last->next = p;
        last= p;
    }
    return head;
};

int check_necklace(int m,struct necklace *head)
{
    struct necklace *p;
    int sumvalue=0,maxvalue=0;
    p = head;
    while(p!=NULL)
    {
        if(maxvalue<p->value)
            maxvalue=p->value;
        sumvalue+=p->value;
        p = p->next;
    }
    sumvalue-=maxvalue;
    return (sumvalue>m);

}
void free_necklace(struct necklace *head)  /* 调用时,形式参数h得到链表的首结点 */
{
    struct necklace *p,*q;
    p = head;
    while(p!=NULL)
    {
        q=p;
        p = p->next;
        free(q);
    }
}
int main()
{
    struct necklace *head;
    int m,n;
    scanf("%d%d",&m,&n);
    head=create_necklace(n);
    if(check_necklace(m,head)==1)
        printf("Yes");
    else
        printf("No");
    free_necklace(head);
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。