Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2187
Questions Jane asked:
Problem Solving Ideas:
Given the total amount of money and the kind of altogether
1. To the given rice, according to the price of ascending, that is, the cheapest rice to the top
2. If the current rice unit Price * Quantity > Total amount, then can buy sum+= total money/Unit Price
Otherwise sum+= the total weight of the current rice, while the total amount of money = Rice Unit Price * Quantity.
3. Output sum, note the format.
AC Code:
#include <stdio.h> #include <stdlib.h>struct n{ int dj,zl;}; int cmp (const void *a,const void *b) { n *a1= (n *) A; n *b1= (n *) b; return A1->DJ-B1->DJ;} int main (void) { int c,tp,i; Double mo,sum; struct N n[1010]; scanf ("%d", &c); while (c--) { sum=0; scanf ("%lf%d", &MO,&TP); for (i=0; i<tp; i++) { scanf ("%d%d", &n[i].dj,&n[i].zl); } Qsort (N,tp,sizeof (n[0]), CMP); for (i=0; i<tp; i++) { if ((N[i].dj*n[i].zl) >=mo) { sum+=mo/(N[I].DJ); break; } else { mo-= (N[I].DJ*N[I].ZL); Sum+=n[i].zl; } } printf ("%.2f\n", sum); } return 0;}
HDU 2187 Mourning 512 Wenchuan earthquake--The old man is really hungry.