Editorial for Bob's Mother


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: acise03

Python

def Recurse(sum_, index):
    if sum_ == N:
        return True
    if index < L - 1:
        index += 1
        if Recurse(sum_, index):
            return True
        elif Recurse(sum_ + sequence[index], index):
            return True
        return False
    return False

N = int(input())
L = int(input())
sequence = list(map(int, input().split()))

if not Recurse(0, -1):
    print("Mom, you tricked me!")
else:
    print("I can do it!")

Java

import java.util.Scanner;

public class Main {
    static int N, L;
    static int[] sequence = new int[1000];

    static boolean Recurse(int sum, int index) {
        if (sum == N)
            return true;
        if (index < L - 1) {
            index++;
            if (Recurse(sum, index)) {
                return true;
            } else if (Recurse(sum + sequence[index], index)) {
                return true;
            }
            return false;
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        L = sc.nextInt();
        for (int i = 0; i < L; i++) {
            sequence[i] = sc.nextInt();
        }
        if (!Recurse(0, -1))
            System.out.println("Mom, you tricked me!");
        else
            System.out.println("I can do it!");
        sc.close();
    }
}

C++

#include <bits/stdc++.h>
using namespace std;
int N, L;
int sequence[1000];
bool Recurse(int sum, int index)
{
    if (sum == N)
        return true;
    if (index < L)
    {
        index++;
        if (Recurse(sum, index))
        {
            return true;
        }
        else if (Recurse(sum + sequence[index], index))
        {
            return true;
        }
        return false;
    }
    return false;
}

int main()
{
    cin >> N >> L;
    for (int i = 0; i < L; i++)
    {
        cin >> sequence[i];
    }
    if (!Recurse(0, -1))
        cout << "Mom, you tricked me!";
    else
        cout << "I can do it!";
    cout << endl;
}

Comments

There are no comments at the moment.