`

FatMouse's Trade

    博客分类:
  • ACM
阅读更多
学了一阵子的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