`
- 浏览:
265201 次
- 性别:
- 来自:
沈阳
-
学了一阵子的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;
}
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
FatMouse' Trade问题主要是说一只老鼠有M磅猫食,然后在N个房间里面用猫食换JavaBean,房间i中有F[i]磅的猫食来换J[i]磅的JavaBean,而且老鼠可以在一个房间里根据一定比例a%来换取JavaBean. 问:如何兑换,才能使得...
PJBlog2 FatMouse模板
1107 FatMouse and Cheese 简单题,不过题目描述有些混乱 1136 Multiple 简单题,BFS 1276 Optimal Array Multiplication Sequence 简单题 1255 The Path 简单题 1250 Always On the Run 简单题 1213 ...
C++的P1,又需要的人请下,很简单,但是是c++的入门程序
PJBlog2 两个人的世界
翁凯java课件 FatMouse
1107 FatMouse and Cheese 简单题,不过题目描述有些混乱 1136 Multiple 简单题,BFS 1276 Optimal Array Multiplication Sequence 简单题 1255 The Path 简单题 1250 Always On the Run 简单题 1213 ...
1001 计算直线的交点数 1002 FatMouse's Speed1003 Common Subsequence1004 Max Sum 1005 Super Jumping! Jumping! Jumping! 1006 免费馅饼 1007 Humble Numbers1008 Monkey and Banana 1009 龟兔赛跑 1010 数塔