使用递归函数但不使用静态或全局变量来查找数字反转的代码。 [英] Code to find a reverse of a number using recursive function but without using static or global variable in that.

查看:93
本文介绍了使用递归函数但不使用静态或全局变量来查找数字反转的代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了下面的代码。

但问题是10的因素应该重复乘以以匹配正确的数字位置。



假设数字是2476然后我想计算反向: -



6 * 1000 + 7 * 100 + 4 * 10 + 2.



但以下代码正在执行



6 + 7 * 10 + 4 * 100 + 2 * 1000 = 2476 (数字本身)



如何在不使用全局或静态变量且不使用math.h函数的情况下使这些10 ^ n正确。



还有人给我提示,使用递归辅助功能可以完成。我还是不能得到它。



我尝试了什么:



I tried the following code.
But problem is of that factor of 10 that that should be multiplied repeatedly to match the correct place of digit.

Suppose number is 2476 then i want to calculate reverse as:-

6*1000 + 7*100 + 4*10 + 2.

But following code is doing

6 + 7*10 + 4*100 + 2*1000 = 2476 (The number itself)

How to get these 10^n correct without using global or static variable and neither using math.h function.

Also someone gave me the hint that using recursive auxiliary function it can be done. I can't still get it.

What I have tried:

#include<stdio.h>

int reverse(int long n){
    int x;
    x = n%10;
    if(n!= 0){
        return (x + reverse(n/10)*10);
    }

    else{
        return 0;
    }
}

int main(void){
    int long n, result;
    printf("Enter a number: ");
    scanf("%ld", &n);
    result = reverse(n);
    printf("\nThe reverse of %ld is %ld\n\n", n, result);

}

推荐答案

10 N 只是你可以得到的结果多次1次10次N次循环,或者甚至更好10次10​​N次-1次。 全局或静态变量的问题是无关紧要的。你永远不需要全局,但你总是需要一个在循环范围之外的变量。 外面并不意味着全球;它只是一个普通的自动(堆栈)变量。事实上,全局或静态变量通常都很糟糕(你明白为什么吗?)...



我希望,这对你来说已经足够了。我不想给你一个完整的解决方案,因为我不想通过这个简单的练习为你学习一些东西带来宝贵的可能性。如果你不自己解决某些问题几乎是不可能的。



此外,似乎没有人限制你只编写一个函数。一般情况下,这是完全不切实际的。所以,你可以添加一个单独的函数来计算10 N ,或者你可能需要的任何其他函数。



-SA
10N is simply the result you could get in a loop by multuplying 1 by 10 N times, or, even better 10 by 10 N − 1 times. The issue of "global or static variable" is irrelevant. You never need global, but you always need a variable outside of the loop scope. "Outside" does not mean "global"; it just a normal auto (stack) variable. Indeed, global or static variables are generally bad (do you understand why?)…

I hope, this will be enough for you. I don't want to give you a complete solution, because I don't want to take out the valuable possibility for you to learn something though this simple exercise. It's nearly impossible to learn if you don't solve certain problems all by yourself.

Also, it looks like no one limits you to writing only one function. It would be utterly impractical, in general case. So, you can add a separate function calculating 10N, or whatever else you may need.

—SA


你的朋友是对的。你需要辅助功能,所以你应该听取他的意见。



基本上,你应该有类似的东西:



Your friend is right. You need an auxiliary function so you should listen his advice.

Essentially, you should have something like:

long reverse_aux(long x, long n) {
  if (n != 0)
  {
    return ??? reverse_aux( ???, n / 10) ???;
  }
  else
  {
    return ???;
  } 
}

long reverse(long n)
{
    return reverse_aux(0, n);
}





由于您的原始代码中几乎有逻辑,因此填写<$ c并不难$ c> ??? 以上。提示:可能会删除 ??? 。您必须弄清楚如何移动部分原始代码。



您可以随时打印代码正在执行的操作以及我对原始代码所做的结果在评论中显示你为什么得到错误答案。



或者使用调试器...



或者使用铅笔和纸......



Since you almost have the logic in your original code, it should not be hard to fill the ??? above. Hint: There might be ??? that should be deleted. You have to figure out how you move part of you original code.

You can always print what the code is doing and the result as I have done with your original code in a comment to show why you get the wrong answer.

Or use a debugger...

Or use a pencil and paper...


你需要跟踪递归中的部分反转。

You need to keep track of partial reverse in the recursion.
#include<stdio.h>

int reverse(int long n, int long r){
	if (n < 10)
	{
		return (r*10+n);
	}
	else
	{
		return reverse(n / 10, r * 10 + n % 10);
	}
}

int main(void){
	int long n, result;
	printf("Enter a number: ");
	scanf("%ld", &n);
	result = reverse(n, 0);
	printf("\nThe reverse of %ld is %ld\n\n", n, result);

}</stdio.h>



如果你不能使用 result = reverse(n,0 ),你需要一个辅助功能。


If you can't use result = reverse(n, 0), you will need an auxiliary function.


这篇关于使用递归函数但不使用静态或全局变量来查找数字反转的代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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