FatMouse's Trade

作者通过学习CUDA认识到C语言、计算机组成原理等课程的重要性,并通过解决一道具体题目加深了对C语言的理解,特别是对指针部分的掌握。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[size=medium]学了一阵子的CUDA,突然有点感概,突然感觉C语言、计算机组成原理、编译原理、微机原理等课程是多么重要,大学浪费了。。现在绝大部分的并行程序设计都是基于C扩展的。谭浩强的那本C学得再久感觉还是没学透,所以今天又上杭电ACM做了一道题,就当复习C语言了。。整个程序编了20分钟,调了一个多小时,C指针真难!!!

FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6744 Accepted Submission(s): 1942
Problem Description

FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.

Input

The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.

Output

For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.

Sample Input

5 37 24 35 220 325 1824 1515 10-1 -1

Sample Output

13.333

31.500




My C code:


#include<stdio.h>
#include<malloc.h>
//节点类型
typedef struct node
{
int j; //存javabean
int f; //存cat food
double per; //存兑换率
struct node * next;
}snode;


//完成插入节点
void insert(snode * &head,int l,int r)
{
double f=l*1.0/r;
snode *p=head->next,*p1,*tmp;
tmp=(snode *)malloc(sizeof(snode));
tmp->j=l;
tmp->f=r;
tmp->per=f;
tmp->next=NULL;
if(p==NULL)
{
tmp->next=NULL;
head->next=tmp;
}
else
{
p1=head;
while(p)
{
if(p->per>f)
{
p1=p;
p=p->next;
}
else
{
p1->next=tmp;
tmp->next=p;
break;
}
}
if(p==NULL)
{
p1->next=tmp;
}
}

}


//完成销毁链表,保留头结点
void destroy(snode * &head)
{
snode *p,*p1;
p=head->next;
head->next=NULL;
while(p!=NULL)
{
p1=p->next;
free(p);
p=p1;
}
if(p!=NULL)
free(p);
}
//完成输入数据和输出结果
int main()
{
int m,n,i,left,right;
double result=0.0,trade=0.0;
snode * head,* p;
head=(snode *)malloc(sizeof(snode));
head->next=NULL;
scanf("%d%d",&m,&n);
while(m!=-1&&n!=-1)
{
result=0.0;trade=0.0;
for(i=1;i<=n;i++)
{
scanf("%d%d",&left,&right);
insert(head,left,right);
}
p=head->next;
/*printf("以下是建立成的链表:\n");
while(p)
{
printf("%d %d %.2f\n",p->j,p->f,p->per);
p=p->next;
}*/
while(p)
{
if((trade+p->f)<=m)
{
result+=p->j;trade+=p->f;p=p->next;
}
else
{
result=result+(m-trade)*p->per;
break;
}
}
printf("%.3f\n",result);
destroy(head);
scanf("%d%d",&m,&n);


}
return 0;
}[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值