简体   繁体   English

如何使用递归查找数字中的最小元素 [C]

[英]How to find the smallest element in a number using a recursion [C]

ok so I am preparing for my C exams and I'm a bit stuck when it comes to recursions I'm a freshman at my university and this seems a bit difficult to me, the exercise requires that in a given number using a recursive function I need to find the smallest element, ex:52873 would be 2 and the program would need to print 2.好的,所以我正在准备我的 C 考试,在递归方面我有点卡住了我需要找到最小的元素,例如:52873 是 2,程序需要打印 2。

#include <stdio.h>

    int min (int number, int element){
        if (number==0)
            return element;
        if (element>number%10)
            element=number%10;

        min(number/10,element);
    }

    int main (){
        int number;
        while (scanf("%d",&number)){
            printf("%d\n",min(number,9));

        }
    }

this is the code for the answer of the exercise but I do not understand it and would love to get some insight on why it is solved like this since I don't really understand it and different ways to solve it, thank you so much in advance.这是练习答案的代码,但我不明白它,并且很想了解为什么它会这样解决,因为我不太了解它以及解决它的不同方法,非常感谢进步。

The idea of the program is the following: 该计划的想法如下:

when you % a number by 10, you get the last digit, ex: 当你将数字乘以10时,你得到最后一位数,例如:

16%10 = 6;
6%10 = 6;
536%10 = 6;

When you / a number by 10, you take out of the number the last digit, ex: 当你/数字乘以10时,你取出最后一位数字,例如:

16/10 = 1;
6/10 = 0; (6 = 06)
536/10 = 53;

So, the idea of your recursive answer is save in "element" the smallest digit, and analyse each digit recursively by dividing the number by 10 (/) and doing module (%). 因此,递归答案的概念是将“元素”保存为最小的数字,并通过将数字除以10(/)和执行模块(%)来递归地分析每个数字。

But, i think your solution have a problem, if my number is 0, you'll return 9 as answer. 但是,我认为你的解决方案存在问题,如果我的数字为0,你将返回9作为答案。

For More Clearity Let's See a Example 为了更清晰,让我们看一个例子

How recursion works 递归如何工作

let's element=9 让我们的元素= 9

it every time check last digit 它每次检查最后一位数

min(9154,element)                       -----first call
if(element>4)       
    element=4       -------//now element is 4               
        min(915,element)    -------9154/10=  915        ------second call

        if(element>5)   ------here element is 4 so  no update

                min(91,element)  ------915/10=91        ------third call

                if(element>1)
                      element=1   ---------now element is 1
                       min(9,element)       ---------91/10=9    -----fourth call

                       if(9>element)     ---------no update 
                            min(0,element)   ------9/10=0  
                        //since become 0 so it return element
int min (int number, int element){
    if (number==0)
        return element;
    if (element>number%10)
        element=number%10;

    min(number/10,element);
}

Here, your recursive function has two arguments. 这里,您的递归函数有两个参数。 int number which stores the number of which the least element is to be obtained and int element to store the least element. int number ,用于存储要获取最少元素的数量; int element用于存储最少元素。

In each call, you check the following two conditions: 在每次通话中,您都要检查以下两个条件:

  • if (number > 0) then return element . if (number > 0)然后返回element
  • if (element > number%10) . if (element > number%10) Here, number % 10 denotes the last number of the number and it's compared against element which is initially set to 9 . 这里, number % 10表示的最后一个数字number和它的对比较element ,其最初被设置为9 If the number % 10 is less than element , then the value of the element is set to number % 10 . 如果number % 10小于element ,则element的值设置为number % 10

After the above process, you recursively call the function with the values number / 10 and element . 在上面的过程之后,您递归调用值为number / 10element的函数。 The reason why you call number \\ 10 is because it removes the last digit from the number which was already checked for in the previous call. 您拨打number \\ 10的原因是因为它删除了上一次呼叫中已经检查过的number的最后一位数字。

And finally, this process goes on till you have no digits are left ie, number becomes zero. 最后,这个过程一直持续到你没有数字为止,即number变为零。

Example: take min(1234, 9) as the call from main() function. 示例:min(1234, 9)作为main()函数的调用。

  1. call from main() function main()函数调用

     number = 1234 element = 9 since number != 0 continue; since, 1234 % 10 ie, 4 < 9, element is set to 4 

    and the function is recursively called with values number = 1234 / 10 = 123 and element = 4 并且函数以值number = 1234 / 10 = 123element = 4递归调用

  2. first recursive function call: 第一个递归函数调用:

     number = 123 element = 4 since number != 0 continue; since, 123 % 10 ie, 3 < 4, element is set to 3 

    and the function is recursively called with values number = 123 / 10 = 12 and element = 3 并且函数以值number = 123 / 10 = 12element = 3递归调用

  3. second recursive function call: 第二个递归函数调用:

     number = 12 element = 3 since number != 0 continue;. since, 12 % 10 ie, 2 < 3, element is set to 2 and the function is recursively called with values `number = 12 / 10 = 1` and `element = 2` 
  4. third recursive function call: 第三个递归函数调用:

     number = 1 element = 2 since number != 0 continue; since, 1 % 10 ie, 1 < 2, element is set to 1 

    and the function is recursively called with values number = 1 / 10 = 0 and element = 1 并且使用值number = 1 / 10 = 0element = 1递归调用函数

  5. finally in the fourth and last recursive call: 终于在第四次也是最后一次递归调用:

     number = 0 element = 1 since `number = 0`, the value of the `element` ie, `1` is returned. 

find the lowest digit of an integer > 0 using recursion:使用递归找到整数 > 0 的最低位:

int min_digit_rec(int num)
{
if (num < 10)
    return num;
else    
        {
            int l_digit;
            int r = min_digit_rec(num / 10);
            
            l_digit = num % 10;
            return r < l_digit ? r : l_digit;
        }
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM