C 使用递归将数字的位数相乘

C 使用递归将数字的位数相乘,c,recursion,C,Recursion,我正在做以下练习: 给定一个四位数字,如3183,将每个数字与最后一个数字进行比较,如果大于或等于,则将其与以下数字相乘 示例:对于编号3183,它将是n=3*8*3=72 我的代码: #include <stdio.h> int f ( int n ) { if ( n < 10 ) return n ; return (((n/10) % 10) >= (n%10) ? ((n/10)10) : 1) * f((n/100 )* 10

我正在做以下练习:

给定一个四位数字,如
3183
,将每个数字与最后一个数字进行比较,如果大于或等于,则将其与以下数字相乘

示例:对于编号
3183
,它将是
n=3*8*3=72

我的代码:

#include <stdio.h>

int f ( int n )
{
    if ( n < 10 ) 
      return n ;
    return (((n/10) % 10) >= (n%10) ? ((n/10)10) : 1) * f((n/100 )* 10 + n % 10 ) ;
}

int main() 
{
    printf( "%d", f( 3183 );

    return(0);
}
#包括
int f(int n)
{
如果(n<10)
返回n;
返回((n/10)%10)>=(n%10)((n/10)10):1)*f((n/100)*10+n%10);
}
int main()
{
printf(“%d”,f(3183);
返回(0);
}

有没有办法缩短或改进它?

编辑我昨天读错了。现在有效地重新创建@Red Alert的答案没有意义,但我也不能删除它,因为它不能被接受,所以就这样吧

我假设我们可以创建自己的“内部”函数来维护状态。我还假设数字是从右侧处理的,原始示例不清楚

static int g(int n, int ack, int last)
{
  const int here = n % 10;
  const bool mult = here >= last;

  if(n < 10)
    return mult ? here * ack : here;
  return g(n / 10, mult ? here * ack : ack, here);
}

int f(int n)
{
  return g(n, 1, 0);
}
static int g(int n,int ack,int last)
{
此处的常数int=n%10;
const bool mult=此处>=最后;
如果(n<10)
返回mult?此处*确认:此处;
返回g(n/10,此处多个*确认:确认,此处);
}
int f(int n)
{
返回g(n,1,0);
}
接受回答后

OP的代码无法编译,缺少
%

//     (((n/10) % 10) >= (n%10) ? ((n/10) 10) : 1) * f((n/100 )* 10 + n % 10 ) ;
return (((n/10) % 10) >= (n%10) ? ((n/10)%10) : 1) * f((n/100 )* 10 + n % 10 ) ;
按照@interjay的建议,保存结果而不是重新计算

#include <stdio.h>

int f(int n) {
  if (n < 10)
    return n;
  int lastdigit = n % 10;
  int nextlastdigit = (n / 10) % 10;
  return (nextlastdigit >= lastdigit ? nextlastdigit : 1)
      * f((n / 100) * 10 + lastdigit);
}

int main(void)   {
    printf( "%u", f(2183); // --> 24
    return(0);
}

是否允许使用中间递归函数?这样可以消除为保持最后一位数字的状态而进行的额外计算:

int f2 ( int n, int lastDigit )
{
    int currentDigit = n%10;
    int returnDigit = currentDigit;
    if(currentDigit < lastDigit)
        returnDigit = 1;
    if(n < 10)
        return returnDigit;

    return returnDigit * f2(n/10, lastDigit );
}

int f ( int n )
{
    if ( n < 10 ) 
      return n ;
    return n%10* f2(n/10, n%10);
}
intf2(intn,intlastDigit)
{
int currentDigit=n%10;
int returnDigit=当前数字;
if(当前位数<最后位数)
返回位数=1;
如果(n<10)
返回数字;
返回位*f2(n/10,最后一位);
}
int f(int n)
{
如果(n<10)
返回n;
返回n%10*f2(n/10,n%10);
}

让另一种方法比原来的方法更紧凑:

#include <stdio.h>

int f (int n, int u)
{
    if (u > n) return(1);
    return (n % 10 >= u ? n % 10 : 1) * f(n/10, u);
}

int main (void)
{
    int n = 3284;
    printf ("%d", f (n , n%10));

    return(0);
}
#包括
整数f(整数n,整数u)
{
如果(u>n)返回(1);
返回(n%10>=u?n%10:1)*f(n/10,u);
}
内部主(空)
{
int n=3284;
printf(“%d”,f(n,n%10));
返回(0);
}

使用变量,而不是多次复制同一个表达式。如果您想要递归解决方案,那么是的,有一个较短的方法。要开始,请考虑如何从任何数字中获取一个数字,然后将该数字与再次调用函数的结果相乘(参数中少一个数字)但是如果收入3283,结果是错误的,输入值应该是722183,我希望结果是24(8*3),就像OP现在的代码一样,但这个答案的结果是48。似乎他没有阅读“将每个数字与最后一个数字进行比较,如果大于或等于,则将其与下面的数字相乘”的规定@RedAlert正确,完全错过了!我们将尝试立即修复它。
int f2 ( int n, int lastDigit )
{
    int currentDigit = n%10;
    int returnDigit = currentDigit;
    if(currentDigit < lastDigit)
        returnDigit = 1;
    if(n < 10)
        return returnDigit;

    return returnDigit * f2(n/10, lastDigit );
}

int f ( int n )
{
    if ( n < 10 ) 
      return n ;
    return n%10* f2(n/10, n%10);
}
#include <stdio.h>

int f (int n, int u)
{
    if (u > n) return(1);
    return (n % 10 >= u ? n % 10 : 1) * f(n/10, u);
}

int main (void)
{
    int n = 3284;
    printf ("%d", f (n , n%10));

    return(0);
}