Mycode:
compile error,i don't know why?
#include#include #include #define maxn 10005using namespace std;struct room{ int j; int f; double val;};struct room r[3001];bool compare(struct room a,struct room b){ return a.val > b.val;}int main(){ double n,m; while(cin >> m >> n) { if(m == -1&&n == -1) break; for(int i = 0;i < n;i ++) { cin >> r[i].j; cin >> r[i].f; r[i].val = (double)r[i].j/r[i].f; } struct room *p = &p[0]; sort(p,p+n,compare); double ans = 0; for(int i = 0;i < n&&m > 0;i ++) { if(m > r[i].f){ ans += r[i].j; m -= r[i].f; } else{ ans += r[i].j * (m/r[i].f); m = 0; } } printf("%.3f\n",ans); } return 0;}
Correct code:
#include#include using namespace std;struct Trade{ int j,f; double percent;}mouse[3001];bool cmp(Trade a,Trade b){ return a.percent>b.percent;}int main(){ int n,m; while(scanf("%d%d",&m,&n)!=EOF&&(n!=-1||m!=-1)) { int i; for(i=0;i mouse[i].f) { sum+=mouse[i].j; m-=mouse[i].f; } else //break; { sum+=mouse[i].percent*m; m=0; break; } } printf("%.3lf\n",sum); } return 0;}