Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- int main() {
- double input;
- cin >> input;
- int change = input * 100;
- int coinCount = 0;
- while (change > 0) { // ИЛИ: for (int i = 0; change > 0; i++) {
- if (change >= 200) {
- change -= 200;
- }
- else if (change >= 100) {
- change -= 100;
- }
- else if (change >= 50) {
- change -= 50;
- }
- else if (change >= 20) {
- change -= 20;
- }
- else if (change >= 10) {
- change -= 10;
- }
- else if (change >= 5) {
- change -= 5;
- }
- else if (change >= 2) {
- change -= 2;
- }
- else {
- change--;
- }
- coinCount++;
- }
- cout << coinCount << endl;
- return 0;
- }
- РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
- #include <iostream>
- #include <string>
- using namespace std;
- int main() {
- double input;
- cin >> input;
- int change = input * 100;
- int coinCount = 0;
- while (change != 0) {
- change -=
- change >= 200 ? 200 :
- change >= 100 ? 100 :
- change >= 50 ? 50 :
- change >= 20 ? 20 :
- change >= 10 ? 10 :
- change >= 5 ? 5 :
- change >= 2 ? 2 : 1;
- coinCount++;
- }
- cout << coinCount << endl;
- return 0;
- }
- РЕШЕНИЕ БЕЗ ЦИКЪЛ:
- #include <iostream>
- #include <string>
- using namespace std;
- int main() {
- double input;
- cin >> input;
- int change = input * 100;
- int coinCount = 0;
- if (change >= 200) {
- coinCount += change / 200;
- change %= 200;
- }
- if (change >= 100) {
- coinCount += change / 100;
- change %= 100;
- }
- if (change >= 50) {
- coinCount += change / 50;
- change %= 50;
- }
- if (change >= 20) {
- coinCount += change / 20;
- change %= 20;
- }
- if (change >= 10) {
- coinCount += change / 10;
- change %= 10;
- }
- if (change >= 5) {
- coinCount += change / 5;
- change %= 5;
- }
- if (change >= 2) {
- coinCount += change / 2;
- change %= 2;
- }
- if (change == 1) {
- coinCount++;
- change--;
- }
- cout << coinCount << endl;
- return 0;
- }
- РЕШЕНИЕ С МАСИВ И FOR:
- #include <iostream>
- #include <string>
- using namespace std;
- int main() {
- double input;
- cin >> input;
- int change = input * 100;
- int coinCount = 0;
- int coinValues[] = { 200, 100, 50, 20, 10, 5, 2, 1 };
- for (int i = 0; i < sizeof(coinValues) / sizeof(int); i++) {
- if (change >= coinValues[i]) {
- coinCount += change / coinValues[i];
- change %= coinValues[i];
- }
- }
- cout << coinCount << endl;
- return 0;
- }
- РЕШЕНИЕ С FOR-EACH: // TARIKAT EDITION:)
- #include <iostream>
- #include <string>
- using namespace std;
- int main() {
- double input;
- cin >> input;
- int change = input * 100;
- int coinCount = 0;
- for (auto coinValue : { 200, 100, 50, 20, 10, 5, 2, 1 }) {
- if (change >= coinValue) {
- coinCount += change / coinValue;
- change %= coinValue;
- }
- }
- cout << coinCount << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement