简体   繁体   English

递归函数,在给定数字中查找偶数或奇数

[英]recursive Function, to find even or odd digits inside given number

Basically, its printing only one instance when it happens, and i don't understand why, maybe has something to do with the code reseting every time and starting the variable at 0 again, and i got another question if someone can help me with, i have to return both values when its odd and even, like how many digits are even and odd at the same time, i'm having a little trouble figuring out how to do it基本上,它在发生时只打印一个实例,我不明白为什么,可能与每次重置代码并再次从 0 开始变量有关,如果有人可以帮助我,我还有另一个问题,我必须在奇数和偶数时返回这两个值,例如有多少位数字同时是偶数和奇数,我在弄清楚如何做时遇到了一些麻烦

#include <stdio.h>

int digits(int n) 
// function that checks if the given value is odd or even, and then add 
// + 1 if it's even, or odd, it's supposed to return the value of the quantity 
// of digits of the number given by the main function
{
    int r;
    int odd  = 0;
    int even = 0;

    r = n % 10;
    if (r % 2 == 0) // check if given number is even
    {
        even = even + 1;
    }
    if (r % 2 != 0) // check if its odd
    {
        odd = odd + 1;
    }

    if (n != 0) {
        digits(n / 10); // supposed to reset function if n!=0 dividing
                // it by 10
    }
    if (n == 0) { return odd; }
}

int
main() // main function that sends a number to the recursive function
{
    int n;
    printf("type number in:\n ");
    scanf("%d", &n);

    printf("%d\n", digits(n));
}

odd and even variables are local in your code, so they are initialized by zero every time. oddeven变量在您的代码中是局部的,因此它们每次都被初始化为零。 I think they should be declared at caller of the recursive function, or be declared as global variables.我认为它们应该在递归函数的调用者处声明,或者声明为全局变量。

#include <stdio.h>
void digits(int n, int *even, int *odd)//function
{
  int r;
  r = n % 10;
  if (r % 2 == 0)//check if given number is even
  {
    *even = *even + 1;
  }
  else //otherwise, its odd
  {
    *odd = *odd + 1;
  }

  n /= 10;
  if (n != 0)
  {
    digits(n, even, odd);//supposed to reset function if n!=0 dividing it by 10
  }
}

int main()
{
  int n, even = 0, odd = 0;
  printf("type number in:\n ");
  scanf("%d", &n);

  digits(n, &even, &odd);

  printf("even: %d\n", even);
  printf("odd: %d\n", odd);

  return 0;
}

Implementing a function that counts the number of odd and even digits in a number, is not to be done using recursive .实现一个计算数字中奇数和偶数位数的函数,不能使用 recursive 来完成 That is simply a wrong design choice.这简直是​​一个错误的设计选择。

But I assume that it's part of your assignment to use recursion so ... okay.但我认为使用递归是你任务的一部分,所以......好吧。

You want a function that can return two values.您需要一个可以返回两个值的函数。 Well, in C you can't!!好吧,在 C 中你不能!! C only allows one return value. C 只允许一个返回值。 So you need another approach.所以你需要另一种方法。 The typical solution is to pass pointers to variables where the result is to be stored.典型的解决方案是将指针传递给要存储结果的变量。

Here is the pseudo-code:这是伪代码:

void count_odd_even(const int n, int *even, int *odd)
{
    if (n == 0) return;
    if (RIGTH-MOST-DIGIT-IN-N-IS-ODD)
    {
        *odd += 1;
    }
    else
    {
        *even += 1;
    }
    count_odd_even(n/10, even, odd);
}

And call it like并称之为

int odd = 0;
int even = 0;
count_odd_even(1234567, &even, &odd);

Maybe I found the problem you are facing.也许我发现了你面临的问题。 You you initialized you odd and even variable as zero.您将奇数和偶数变量初始化为零。 every time you call the function it redeclares their value to zero again.每次调用该函数时,它都会再次将它们的值重新声明为零。 You can use pointer caller or use those as your global variable so that every time they don't repeat their initial values again.您可以使用指针调用者或将它们用作全局变量,以便每次它们都不会再次重复其初始值。

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

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