Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- int main() {
- double budget, price, totalSum = 0;
- cin >> budget;
- cin.ignore();
- string product;
- getline(cin, product);
- int counter = 0;
- while (product != "Stop") {
- cin >> price;
- cin.ignore();
- counter++;
- if (counter % 3 == 0) {
- price /= 2;
- }
- totalSum += price;
- if (budget < totalSum) {
- break;
- }
- getline(cin, product);
- }
- cout.setf(ios::fixed);
- cout.precision(2);
- if (product == "Stop") {
- cout << "You bought " << counter << " products for " << totalSum << " leva.\n";
- }
- else {
- cout << "You don't have enough money!\n"
- << "You need " << totalSum - budget << " leva!\n";
- }
- return 0;
- }
- Решение с for и printf():
- #include <iostream>
- #include <string>
- using namespace std;
- int main() {
- double budget, price, totalSum = 0;
- cin >> budget;
- cin.ignore();
- string product;
- getline(cin, product);
- for (int i = 1; i < 2147483647; i++) {
- if (product == "Stop") {
- printf("You bought %i products for %.2f leva.\n", --i, totalSum);
- return 0;
- }
- cin >> price;
- cin.ignore();
- if (i % 3 == 0) {
- price /= 2;
- }
- totalSum += price;
- if (budget < totalSum) {
- printf("You don't have enough money!\nYou need %.2f leva!\n", totalSum - budget);
- return 0;
- }
- getline(cin, product);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement