计算特定数字并使用递归用 0 替换偶数数字 [英] Count specific digits and replace even digits with 0 using recursion

查看:22
本文介绍了计算特定数字并使用递归用 0 替换偶数数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C++ 新手,这是我的第一个艰巨任务,我在理解递归时遇到了困难.我需要编写一个递归函数来计算用户输入 5 和 8 的次数.以及用 0 替换偶数位.示例:

I am new to C++, this is my first hard assignment and I am having trouble understanding recursion. I need to program a recursive function to count how many times a user enters 5 and 8. As well as replace even digits with 0. Example:

用户输入:4585

计数 5 &8:3

Count of 5 & 8: 3

甚至替换为 0: 0505

Replace even with 0: 0505

下面的代码完全错误,但这是我需要使用的语言.并注意它必须如何包含递归.我可以稍后调用该函数.

The code below is completely wrong but this is the language I need to use. And take note how it must include recursion. And I can call upon the function later on.

#include <iostream> 
using namespace std; 

int countThreeFives(int num) {
    static int count=0;

    int digit = num % 10; 
    if (n == 5 || n == 8)
    {
        count++;
        countThreeFives(num/10);
    }
    else
    {
        return count;
    }
}

int replaceEven(int num){ 
    int digit = num % 10;
    if (digit % 2 != 0) {
    // Confused here (incomplete)
    } 
int main() { 
    int x; 
    cout << "Enter a positive number: "; 
    cin >> x;

    cout << "The count of 8 & 5 is: " << countThreeFives(x); 
    cout << "Replacing even with 0: " << replaceEven(x);
}

推荐答案

我猜这是一个学校作业,因此需要在这里使用递归.无论如何,这是一个递归解决方案:

I'm guessing this is a school assignment hence the requirement to use recursion here. Anyways, here is a recursive solution:

让我们用 f(d, n) 表示一串数字 d 的前 n 位数字中 5 和 8 的个数.那么我们可以形成如下递归关系:

Let us denote by f(d, n) the number of 5s and 8s in the first n digits of a string of digits d. Then we can form the following recursive relation:

f(d, n) = 1 + f(d, n - 1) 如果第 n 个数字是 5 或 8

f(d, n) = 1 + f(d, n - 1) if the nth digit is either 5 or 8

f(d, n) = f(d, n - 1) 如果第 n 个数字既不是 5 也不是 8

f(d, n) = f(d, n - 1) if the nth digit is neither a 5 nor an 8

我们的基本情况是 f(d, 0) = 0,因为大小为 0 的字符串将没有 5s 和 8s

and our base case is f(d, 0) = 0, since a string of size 0 will have no 5s and no 8s

#include <iostream>
#include <string>

int countAndReplace(std::string& digits, int n)
{
    if(n == 0)
    {
        return 0;
    }
    bool addOne = (digits[n - 1] == '5' || digits[n - 1] == '8');
    if(digits[n - 1] % 2 == 0)
    {
        digits[n - 1] = '0';
    }
    return addOne + countAndReplace(digits, n - 1);
}

int main()
{

    std::string digits;

    std::cin >> digits;

    std::cout << countAndReplace(digits, digits.size()) << '\n';

    std::cout << digits << '\n';

    return 0;
}

首先我们需要从标准输入中读取数字,最好使用 std::string,因为我们事先不知道数字的数量.然后我们调用我们的递归函数,它接受两个参数 - 对数字字符串的引用(用于在任务的第二部分更改适当的数字)和所述字符串的长度.在函数中,对于每个数字,我们还会根据您发布的规则检查它是否需要替换,如果需要,我们会进行替换,因此为什么将对该字符串的引用用作参数.

First we need to read the digits from standard input for which it is best to use a std::string since we don't know the number of digits in advance. Then we call our recursive function that takes two arguments - a reference to the string of digits (used for changing the digits in place, for the second part of the task), and the length of said string. In the function, for each digit we also check if it needs to be replaced according to the rules you posted, and if it does we do the replacement in place, hence why a reference to the string is used as an argument.

这是一个非常简单的问题.下次请在寻求帮助之前尝试自己解决作业.否则你会很难进步.

This is a very easy problem. Please, next time, try solving your assignments by yourself before asking for help. Otherwise you will have trouble progressing.

这篇关于计算特定数字并使用递归用 0 替换偶数数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆