SuitNdtie

Rice PROG-1030

Mar 10th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<algorithm>
  3. using namespace std;
  4.  
  5. typedef struct{
  6.     double price;
  7.     double quantity;
  8.     double ppq;
  9. }rice;
  10.  
  11. bool mycmp(rice a,rice b){
  12.     return a.ppq < b.ppq;
  13. }
  14.  
  15. int main()
  16. {
  17.     int k;
  18.     scanf("%d",&k);
  19.     rice arrice[k];
  20.     for(int i=0;i<k;i++){
  21.         scanf("%lf %lf",&arrice[i].price,&arrice[i].quantity);
  22.         arrice[i].ppq = arrice[i].price/arrice[i].quantity;
  23.     }
  24.     sort(arrice,arrice+k,mycmp);
  25.     int m;
  26.     scanf("%d",&m);
  27.     int j = 0;
  28.     for(int i=0;i<m;i++){
  29.         double x;
  30.         double ans = 0;
  31.         scanf("%lf",&x);
  32.         while(x > 0){
  33.             if(arrice[j].quantity >= x){
  34.                 ans += arrice[j].ppq*x;
  35.                 arrice[j].quantity -= x;
  36.                 x = 0;
  37.             }else{
  38.                 ans += arrice[j].ppq*arrice[j].quantity;
  39.                 x -= arrice[j].quantity;
  40.                 arrice[j].quantity = 0;
  41.             }
  42.             if(arrice[j].quantity == 0){
  43.                 j++;
  44.             }
  45.         }
  46.         printf("%.3lf\n",ans);
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment