我是一名初学程序员,正在尝试Codility frog jump问题。这是我的代码解决方案:
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的数字范围的要求时。如果我将上面的内容更改为2000000,则会返回负数。 unsigned long int应该返回一个正数但是当我使用2000000时它返回负数。
答案 0 :(得分:2)
这是因为C ++中的int数字有限制 - 请参阅http://www.cplusplus.com/reference/climits/。在大多数情况下,标准类型应满足您的需求。
如果标准类型不够大,请参阅What's the best (for speed) arbitrary-precision library for C++?
这是一个更简单的代码版本,您怎么看?
unsigned long int solution( int currentPosition, int positionToGetTo , int stepsJumpedEachTime) {
if (currentPosition >= positionToGetTo)
return 0;
return 1 + solution(currentPosition + stepsJumpedEachTime, positionToGetTo, stepsJumpedEachTime);
}
int main(){
int x = 10;
int y = 85;
int d = 30;
unsigned long int stepsTaken = solution(x, y, d);
cout << "Total Steps Taken: " << stepsTaken << endl;
}
答案 1 :(得分:2)
迭代需要很长时间。
int destination = Y - X;
int steps = destination / D;
int remainder = destination % D;
if (remainder != 0){
steps++;
}
return steps;
答案 2 :(得分:1)
在不使用递归的情况下使其工作如下:
#include <iostream>
#include <iostream>
using namespace std;
unsigned long int solution( int currentPosition,int positionToGetTo ,int stepsJumpedEachTime);
int main(){
int x = 10;
int y = 1000000000;
int d = 30;
unsigned long int stepsTaken = solution(x, y, d);
cout << "Total Steps Taken: " << stepsTaken << endl;
}
unsigned long int solution(int currentPosition, int positionToGetTo ,int stepsJumpedEachTime){
unsigned long int stepsTaken = 1;
currentPosition += stepsJumpedEachTime;
while (currentPosition < positionToGetTo){
currentPosition += stepsJumpedEachTime;
stepsTaken++;
}
return stepsTaken;
}
答案 3 :(得分:0)
我希望这会奏效:
class Solution {
public int solution(int X, int Y, int D) {
if(X>=Y)
return 0;
if((Y-X)%D!=0) return (Y-X)/D+1;
else
return (Y-X)/D;
}
}
答案 4 :(得分:0)
100%“浮动”版本,在我的x64 CPU上比“int”版本快三倍:
#include <cmath>
int solution(int X, int Y, int D)
{
return std::floor( (double)( Y - 1 - X ) / D + 1 );
}
答案 5 :(得分:0)
C解决方案。 Codility的100分中有100分
int FrogJmp(int X, int Y, int D) {
int result = 0;
int dest = Y - X;
result = dest / D;
if (dest % D != 0) {
result++;
}
return result;
}
答案 6 :(得分:0)
使用ceil的解决方案:
#include <cmath>
#include <climits>
int solution(int X, int Y, int D)
{
if (X >= Y)
return 0;
if (D == 0)
return INT_MAX;
return std::ceil((double)(Y - X) / D);
}
答案 7 :(得分:0)
100%得分 - C代码:Codility:FrogJmp
int solution(int X, int Y, int D) {
// write your code in C90
if (Y == X)
return 0;
else
return (((Y - X) / D) + (((Y - X) % D) > 0 ? 1 : 0));
}
预期最坏情况时间复杂度为O(1)表示无迭代...