2

I am trying to convert int 123 to String "CBA" in java

I know I have some something misunderstood in recursion

After many println statements, I know that at a point

ss is going to be "CBA"

but then it will return "CB" and "C"

I kind of know recursion will work like this, but I don't know how to fix it

Can anyone help me with the code, thanks!

public String recur ( int x, String ss )
{
    if ( x >= 1 && x < 10 )
        ss = ss + "A";

    if ( x >= 10 && x < 100 )
    {
        ss = ss + "B";
        recur( x % 10, ss);
    }

    if ( x >= 100 && x < 1000 )
    {
        ss = ss + "C";
        recur( x % 100, ss);    
    }
return ss;          
4

3 回答 3

1

您忽略了 recur 的返回值,因此在输出中只能看到第一级递归。

因此,您可以这样做以考虑返回值。(只需将recur的输出分配给ss)

ss = recur( x % 10, ss);

和另一个电话。

ss = recur( x % 100, ss);
于 2013-06-01T01:49:08.627 回答
0

您的方法不正确,对于初学者来说,您甚至不需要将字符串作为参数传递 - 只需在返回值中构建字符串即可。此外,从整数转换为字符串的逻辑过于复杂,并且在整数包含1, 2, 3精确顺序的数字时才有效!这是一种更简单的工作方法,它依次处理每个字符并适用于任何数字的整数:

public String recur(int x) {
    if (x <= 0)
        return "";
    return (char) (x % 10 + 'A' - 1) + recur(x / 10);
}

解释:

  • 当整数等于或小于零时发生基本情况,在这种情况下我们必须返回一个空字符串
  • 递归步骤的工作原理如下:
    • 我们通过取余数从整数中获取当前数字:x % 10,并通过添加将数字转换为大写字符'A' ,并通过加减1
    • 我们将前一个结果与推进递归的结果连接起来,将整数除以10
于 2013-06-01T01:57:45.380 回答
0

(This Answer is largely for the peanut gallery ...)

Here's my take on the "best" recursive solution to the problem as stated:

public String recur ( int x )
{
    if ( x >= 1 && x < 10 ) {
        return "A";
    } else if ( x >= 10 && x < 100 ) {
        return "B" + recur( x );
    } else if ( x >= 100 && x < 1000 ) {
        return "C" + recur( x );    
    } else {
        throw new IllegalArgumentException();
    }
}

System.err.println(recur(123));

or a bit more generally:

public String recur ( int x)
    return recur0( x, 0 );
}

public String recur0 ( int x, int i )
{
    if ( x <= 0 ) {
        return "";
    else {
        return recur0( x % 10, i + 1 ) + ((char) ('A' + i));
    } 
}

System.err.println(recur(123));

As you can see, no StringBuilder is needed. At best, a StringBuilder is a micro-optimization. It does not make the code any safer, or any more readable. To illustrate, here's the first version redone using a StringBuilder

public void recur ( int x, StringBuilder sb )
{
    if ( x >= 1 && x < 10 ) {
        sb.append("A");
    } else if ( x >= 10 && x < 100 ) {
        sb.append("B");
        recur( x, sb );
    } else if ( x >= 100 && x < 1000 ) {
        sb.append("C");
        recur( x, sb );    
    } 
}

StringBuilder sb = new StringBuilder();
recur(123, sb);
System.err.println(sb.toString());

But the simplest solution for the problem as originally stated is this:

public String nonRecur(int x) {
    if ( x >= 1 && x < 10 ) {
        return "A";
    } else if ( x >= 10 && x < 100 ) {
        return "BA";
    } else if ( x >= 100 && x < 1000 ) {
        return "CBA";    
    } else {
        throw new IllegalArgumentException();
    }
}

System.err.println(nonRecur(123));

Note that there is considerable ambiguity in the way that the problem was stated, and @ÓscarLópez's solution is based on a different interpretation of the problem than mine. He is mapping the digits of the numbers to letters, rather than using the letters to signify magnitude.

In fact, the OP's actual problem is neither of these interpretations, and I don't think that any of the Answers will actually help him ... even though they do answer his Question.

于 2013-06-01T02:53:57.283 回答