水提,不讲太多,就是最基础的贪心算法
代码如下:
- #include<iostream>
- #include<cstdio>
- #include<iomanip>
- usingnamespacestd;
- typedefstructNode
- {
- intp;
- inth;
- }data;
- intComp(constvoid*p1,constvoid*p2)
- {
- return((Node*)p2)->p<((Node*)p1)->p?1:-1;
- }
- intmain()
- {
- intc;
- datarice[1000];
- cin>>c;
- while(c)
- {
- floatbuy=0;
- intn,m;
- cin>>n>>m;
- for(inti=0;i<m;i++)
- cin>>rice[i].p>>rice[i].h;
- qsort(rice,m,sizeof(data),Comp);
- //for(i=0;i<m;i++)
- //cout<<rice[i].p<<""<<rice[i].h<<endl;
- for(i=0;i<m;i++)
- {
- if(n<rice[i].h*rice[i].p)
- {
- buy+=(float)n/rice[i].p;
- break;
- }
- else
- {
- buy+=rice[i].h;
- n=n-rice[i].h*rice[i].p;
- }
- }
- printf("%.2f/n",buy);
- c--;
- }
- return0;
- }