Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- const int BUS_CARGO_PRICE_PER_TON = 200;
- const int TRUCK_CARGO_PRICE_PER_TON = 175;
- const int TRAIN_CARGO_PRICE_PER_TON = 120;
- int cargoCount;
- cin >> cargoCount;
- int busCargo = 0, truckCargo = 0, trainCargo = 0;
- int currentCargo;
- for (int i = 1; i <= cargoCount; i++) {
- cin >> currentCargo;
- if (currentCargo < 4) {
- busCargo += currentCargo;
- }
- else if (currentCargo < 12) {
- truckCargo += currentCargo;
- }
- else {
- trainCargo += currentCargo;
- }
- }
- int totalCargo = busCargo + truckCargo + trainCargo;
- double average = 1.0 * (busCargo * BUS_CARGO_PRICE_PER_TON + truckCargo * TRUCK_CARGO_PRICE_PER_TON + trainCargo * TRAIN_CARGO_PRICE_PER_TON) / totalCargo;
- cout.setf(ios::fixed);
- cout.precision(2);
- cout << average << "\n";
- cout << 100.0 * busCargo / totalCargo << "%\n";
- cout << 100.0 * truckCargo / totalCargo << "%\n";
- cout << 100.0 * trainCargo / totalCargo << "%\n";
- return 0;
- }
- Решение с тернарен оператор:
- #include <iostream>
- using namespace std;
- int main() {
- const int BUS_CARGO_PRICE_PER_TON = 200;
- const int TRUCK_CARGO_PRICE_PER_TON = 175;
- const int TRAIN_CARGO_PRICE_PER_TON = 120;
- int cargoCount;
- cin >> cargoCount;
- int busCargo = 0, truckCargo = 0, trainCargo = 0, currentCargo;
- for (int i = 1; i <= cargoCount; i++) {
- cin >> currentCargo;
- (currentCargo < 4 ? busCargo : currentCargo < 12 ? truckCargo : trainCargo) += currentCargo;
- }
- int totalCargo = busCargo + truckCargo + trainCargo;
- double average = 1.0 * (busCargo * BUS_CARGO_PRICE_PER_TON + truckCargo * TRUCK_CARGO_PRICE_PER_TON + trainCargo * TRAIN_CARGO_PRICE_PER_TON) / totalCargo;
- cout.setf(ios::fixed);
- cout.precision(2);
- cout << average << "\n";
- cout << 100.0 * busCargo / totalCargo << "%\n";
- cout << 100.0 * truckCargo / totalCargo << "%\n";
- cout << 100.0 * trainCargo / totalCargo << "%\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement