[Netease] 2023 Autumn Recruitment Written Examination (Machine Learning Algorithm Post)

[Netease] 2023 Autumn Recruitment Written Examination (Machine Learning Algorithm Post) 1. Magic Coin

DESCRIPTION

Xiao Yi is going to the Magic Kingdom to purchase magic artifacts. To buy magic artifacts, you need to use magic coins, but Xiao Yi does not have any magic coins now, but Xiao Yi has two magic machines that can generate more magic coins by investing x (x can be 0) magic coins.
Magic machine 1: If x magic coins are put in, the magic machine will turn them into 2x+1 magic coins. Magic
machine 2: If x magic coins are put in, the magic machine will turn them into 2x+2 magic coins. Xiao Yi needs
n magic coins in total to purchase magic artifacts, so Xiao Yi can only generate exactly n magic coins through two magic machines. Xiao Yi needs you to help him design an investment plan so that he has exactly n magic coins in the end.

INPUT

The input includes one line, including a positive integer n (1 ≤ n ≤ 10^9), indicating the number of magic coins that Xiao Yi needs.

OUTPUT

Output a character string, each character represents the magic machine that Xiaoyi chooses to invest in this time. Which only contains the characters '1' and '2'.

SAMPLE INPUT

10

SAMPLE OUTPUT

122

Problem-solving ideas:

The first idea is to search deeply

#include <iostream>

using namespace std;

void dfs(string &res, string &path, int sum, int target) {
    if (sum > target) {
        path.back();
        return;
    }
    if (sum == target) {
        if (res == "") {
            res = path;
        }
        return;
    }
    path += '1';
    dfs(res, path, 2 * sum + 1, target);
    pa

Guess you like

Origin blog.csdn.net/yetaodiao/article/details/131810407