奇怪的(对我来说)退货声明-在Google上找不到与此有关的任何信息

时间:2018-11-04 17:21:08

标签: c++ recursion

我使用递归编写了一个C ++程序,该程序返回数字中的最大数字。
我按照自己的方式进行了工作,但找到了替代方法:

int cifmax(int n) {
    if(n <= 9)
        return n;
    return max(n % 10, cifmax(n / 10));
}

第二次返回如何工作?

1 个答案:

答案 0 :(得分:2)

您可以认为此代码等效于以下更详细的版本:

 /**
 * WaterfallDialogStep to process the user's picture.
 * @param {WaterfallStepContext} step WaterfallStepContext
 */
async processPhotoStep(step) {
    await this.writeLogInTheStorage('Start downloading picture....');
    await this.handleIncomingAttachment(step);
    return await step.endDialog();
};
/**
 * responds to the user with information about the saved attachment or an error.
 * @param {Object} turnContext 
 */
async handleIncomingAttachment(step) {
    // Prepare Promises to download each attachment and then execute each Promise.
    const attachment = step.context.activity.attachments[0];
    const tokenIsRequired = await this.checkRequiresToken(step.context);
    const dc = await this.dialogs.createContext(step.context);
    const token = await dc.beginDialog(LOGIN_PROMPT); //await step.context.adapter.getUserToken(step.context, CONNECTION_SETTING_NAME);
    let file = undefined;
    if (tokenIsRequired) {
        file = await this.downloadAttachment(token.result.token, attachment.contentUrl);
    }
    else {
        file = await requestX(attachment.contentUrl);
    }
    await OAuthHelpers.postPhoto(step.context, token.result, file);
}

async downloadAttachment(token, url) {
    const p = new Promise((resolve, reject) => {
        request({
            url: url,
            headers: {
                'Authorization': 'Bearer ' + token,
                'Content-Type': 'application/octet-stream'
            }
        }, async function (err, response, body) {
            const result = body

            if (err) {
                console.log(err);
                //await this.writeLogInTheStorage('err 1 : ' + err);
                reject(err);
            } else if (result.error) {
                console.log(result.error);
                //await this.writeLogInTheStorage('err 2 : ' + err);
                reject(result.error.message);
            } else {
                // The value of the body will be an array.
                console.log(result);
                //await this.writeLogInTheStorage('success : ' + result);
                resolve(result);
            }
        });
    });
    return p;
}

请注意以下代码段:

int cifmax(int n) {
    if (n <= 9){
        return n; // trivial base case
    } else {
        int currentdigit = n % 10; // the least significant digit
        int otherdigits = n / 10; // other digits past the least significant
        int maxofotherdigits = cifmax(otherdigits); // make the recursive call

        // compute maximum of the two
        if (currentdigit > maxofotherdigits){
            return currentdigit;
        } else {
            return maxofotherdigits;
        }
    }
}

等效于:

if (currentdigit > maxofotherdigits){
    return currentdigit;
} else {
    return maxofotherdigits;
}

其中std::max返回其两个参数中的较大者。