
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to check we can buy product with given money or not
Suppose we have a number N. A cake seller is selling cakes with 40 rupees, and doughnuts with 70 rupees each. We have to check whether we can buy some of them with exactly N rupees or not.
So, if the input is like N = 110, then the output will be True, because 40 + 70 = 110.
To solve this, we will follow these steps −
o := false Define a function dfs(), this will take i, if i > n, then: return false if i is same as n, then: return true if dfs(i + 40), then: return true return dfs(i + 70) From the main method, do the following n := N o := dfs(0) return o
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int n; bool o = false; bool dfs(int i) { if (i > n) return false; if (i == n) return true; if (dfs(i + 40)) return true; return dfs(i + 70); } bool solve(int N) { n = N; o = dfs(0); return o; } int main(){ int N = 110; cout << solve(N) << endl; }
Input
110
Output
1
- Related Articles
- C++ Program to check given candies can be split with equal weights or not
- C++ program to check we can rearrange array in such a way that given formula returns m or not
- Python program to check whether we can pile up cubes or not
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Program to check we can reach leftmost or rightmost position or not in Python
- Program to check whether we can take all courses or not in Python
- Program to check we can cross river by stones or not in Python
- Program to check whether we can unlock all rooms or not in python
- Program to check we can form array from pieces or not in Python
- C++ program to check we can place dominos on colored cells in correct order or not
- C++ Program to check if given numbers are coprime or not
- C++ program to check whether given string is bad or not
- C++ Program to check whether given password is strong or not
- Program to check we can replace characters to make a string to another string or not in C++
- Program to check whether we can get N queens solution or not in Python

Advertisements