숫자 N이 있다고 가정합니다. 케이크 판매자가 각각 40루피의 케이크와 70루피의 도넛을 판매하고 있습니다. 정확히 N 루피로 몇 개를 살 수 있는지 확인해야 합니다.
따라서 입력이 N =110과 같으면 40 + 70 =110이므로 출력은 True가 됩니다.
이 문제를 해결하기 위해 다음 단계를 따릅니다. −
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
예시
이해를 돕기 위해 다음 구현을 살펴보겠습니다. −
#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;
} 입력
110
출력
1