Click Here to go back to the homepage.

Scaling Recipes Solution:


#include <bits/stdc++.h>
using namespace std;


int main(){
    // #ifndef TESTING
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif

    int nCases;
    cin >> nCases;
    cout << fixed << setprecision(4);
    for(int caseNum = 1; caseNum <= nCases; caseNum++){
        int nIngredients, recipePortions, desiredPortions;
        cin >> nIngredients >> recipePortions >> desiredPortions;
        double scaleFactor = desiredPortions / (double) recipePortions;
        pair<string, double> ingredients[nIngredients];
        double mainIngredientWeight;
        double scaledWeight;
        string mainItem;
        for(int i = 0; i < nIngredients; i++){
            string item;
            double weight, percentage;
            cin >> item >> weight >> percentage;
            if(percentage == 100){
                mainIngredientWeight = weight;
                scaledWeight = scaleFactor * weight / 100;
                mainItem = item;
            }
            ingredients[i] = {item, percentage};
        }
        cout << "Recipe # " << caseNum << endl;
        for(int i = 0; i < nIngredients; i++){
            if(ingredients[i].first == mainItem){
                cout << mainItem << " " << scaledWeight * 100 << endl;
            } else {
                cout << ingredients[i].first << " " << scaledWeight * ingredients[i].second << endl;
            }
        }
        cout << "----------------------------------------" << endl;
    }
    
    return 0;   
}