Palindrome函数使用递归

时间:2016-03-20 15:50:06

标签: c++ recursion palindrome

我无法看到我在这里出错了,它为一些回文工作但不为别人工作。

基本上程序接受一个单词,单词的长度然后返回如果它是回文或不是回文,并且该函数必须使用递归。

public class FlipAnimation extends Animation {

...

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    // Angle around the y-axis of the rotation at the given time
    // calculated both in radians and degrees.
    final double radians = Math.PI * interpolatedTime;
    float degrees = (float) (180.0 * radians / Math.PI);

    // Once we reach the midpoint in the animation, we need to hide the
    // source view and show the destination view. We also need to change
    // the angle by 180 degrees so that the destination does not come in
    // flipped around
    if (interpolatedTime >= 0.5f) {
        degrees -= 180.f;
        fromView.setVisibility(View.GONE);
        toView.setVisibility(View.VISIBLE);
    }

    if (forward)
        degrees = -degrees; //determines direction of rotation when flip begins

    final Matrix matrix = t.getMatrix();
    camera.save();
    if(horizontal){
        camera.rotateX(degrees);
    } else {
        camera.rotateY(degrees);
    }
    camera.getMatrix(matrix);
    camera.restore();
    matrix.preTranslate(-centerX, -centerY);
    matrix.postTranslate(centerX, centerY);
}

任何人都可以看到此功能有任何问题吗?

1 个答案:

答案 0 :(得分:0)

我的鸭子说你非常接近。他说你需要忘记你试图用start做的任何事情,并且每次递归都会将长度减少2而不是1(因为第一个字符与其匹配,最后一个字符配对)。然后他告诉我这个:

bool palindrome(const char* a, int length) {
    if(length < 2) return true;
    if(a[0] != a[length-1]) return false;
    return palindrome(a+1, length - 2);
}