以相反顺序的正十进制整数的递归函数数字 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12270843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
recursive function digits of a positive decimal integer in reverse order c++
提问by Kayla Bianchi
I have an assignment to write a recursive function that writes the digits of a positive integer in reverse order. My problem is that the function doesn't display the reverse correctly. I know im supposed to use % or 10 when displaying the number and / of 10 when in the recursive call as well as the base case is supposed to be < 10. Here is my code.
我有一个作业要编写一个递归函数,该函数以相反的顺序写入正整数的数字。我的问题是该函数不能正确显示反向。我知道我应该在递归调用中显示 10 的数字和 / 时使用 % 或 10 以及基本情况应该 < 10。这是我的代码。
#include <iostream>
using namespace std;
int reverse(int,int);
int main()
{
int number;
int n;
cout << " Enter number to reverse." << endl;
cin >> number;
cout << reverse(number % 10,0);
return 0;
}//end main
int reverse(int number,int n)
{
if(n < 10)
{
return n;
}
else
{
return reverse(number/10,n);
}
}// end reverse
回答by Rami Jarrar
I think this is what your function should be:
我认为这就是您的功能应该是:
void reverse(int number){
if(number == 0) //base/basic case i.e if number is zero the problem is already solved, nothing to do, so simply return
return;
else{
cout << number % 10; // print that last digit, e.g 103%10 == 3
reverse(number/10); //solve the same problem but with smaller number, i.e make the problem smaller by dividing it by 10, initially we had 103, now 10
}
}
回答by Greg
You could use following code (if you do not mind striping leading zeros, or you could accumulate chars in string or ostringstream)
您可以使用以下代码(如果您不介意去除前导零,或者您可以在字符串或 ostringstream 中累积字符)
unsigned reverse(unsigned n, unsigned acc)
{
if (n == 0)
{
return acc;
}
else
{
return reverse(n / 10, (acc * 10) + (n % 10));
}
}
unsigned reverse(unsigned n)
{
return reverse(n, 0);
}
回答by ch0kee
This solution will omit trailing zeroes, because it is literally reversing the content of the integer:
此解决方案将省略尾随零,因为它实际上是在反转整数的内容:
int reverse(int number, int n = 0)
{
if (number == 0)
{
return n;
}
else
{
int nextdigit = number%10;
int nextprefix = n*10+nextdigit;
return reverse(number/10 ,nextprefix);
}
}
回答by Naheel
int rev(int n) {
if(n<10&&n>-10) return n;
int length=0;
for (int i=n; i; i/=10) length++;
return n%10*(int)pow(10, length-1) + rev(n/10);
}
Here's my solution. It takes only one parameter and return an int. Also don't forget to include cmath.
这是我的解决方案。它只需要一个参数并返回一个整数。也不要忘记包括 cmath。
int intLength(int i) {
int l=0;
for(;i;i/=10) l++;
return l;
}
int rev(int n) {
return n<10&&n>-10 ? n : n%10*(int)pow(10, intLength(n)-1) + rev(n/10);
}
Or this way it's a bit more elegant.
或者这样更优雅一些。
回答by jviotti
You could also do:
你也可以这样做:
int reverse(int number,int n) {
if(number > n) {
cout << number << endl;
reverse(number-1,n);
}
But you should get rid of first number printing twice.
但是你应该去掉第一个数字打印两次。