我是一名初学者程序员,正在尝试 Codility 青蛙跳问题。这是我的代码解决方案:
int solution(int, int, int, unsigned long int&);
int main(){
unsigned long int stepsTaken = 1;
int x = 10;
int y = 85;
int d = 30;
solution(x, y, d, stepsTaken);
cout << "Total Steps Taken: " << stepsTaken << endl;
}
int solution( int X, int Y, int D, unsigned long int &stepsTaken) {
int currentPosition = X;
int positionToGetTo = Y;
int stepsJumpedEachTime = D;
currentPosition += stepsJumpedEachTime;
if(currentPosition < positionToGetTo){
stepsTaken++;
solution(currentPosition, positionToGetTo, stepsJumpedEachTime, stepsTaken);
}
return stepsTaken;
}
现在我遇到的问题是,当我尝试满足处理 1-1000000000 范围内的数字的要求时。如果我将上面的 int y 更改为 2000000,我会得到一个否定的返回值。unsigned long int 应该返回一个正数,但是当我使用 2000000 时它返回负数。