题目描述
只需要提交补充的那一部分代码,提交时选择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
参考代码
#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(p->value>maxvalue)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;
}
解析
暂无