 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements
 