当数组中的数字为1时,我的代码是+1得分,当数组中的数字是-1时,我的代码是-1。但是当我运行程序时它只返回0.递归本身并没有无限循环,我不知道我在这里做错了什么。
#include <iostream>
using namespace std;
/* When the tile is -1, score--. When the tile is 1, score++. You cannot go outside the array nor can you go backwards */
/* + 2 through the array */
int jumping( int track[], int size, int location ){
static int score = 0;
if ( track[ location ] == 1 ){
score++;
}
else if ( track[ location ] == -1 ){
score--;
}
if (( location == size - 1 ) || ( track[ location + 1 ] == 0 )){
return score;
}
else{
jumping( track, size, location + 2 );
}
}
/* Run through the array by 1 */
int stepping( int track[], int size, int location ){
static int score = 0;
if ( track[ location ] == 1 ){
score++;
}
else if ( track[ location ] == -1 ){
score--;
}
if ( location == size - 1 ){
return score;
}
else{
stepping( track, size, location++ );
}
}
/* Is to calculate the maxium possible score for any given track, with any arrangement using recursion. */
int maxScore( int track[], int size, int location ){
int score = 0; //Keep track of score
int step = stepping( track, size, location );//score by stepping
int jump = jumping( track, size, location );//score by jumping
/* If step is higher or jump and replace according to the highest */
if ( step > jump ){
score = step;
return score;
}
else if ( jump > step ){
score = jump;
return score;
}
else if ( jump == step ){
return score;
}
}
int main(){
int simple[2] = { 0, 0 };
int easy[8] = { 0, -1, 1, 1, -1, 1, -1, 0 };
int medium[25] = { 0, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1, 0 };
int mediumhard[3] = { 0, 1, 0 };
int impossible[20] = { 0, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 0 };
int insane[40] = { 0, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1,
1, -1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, -1, 0 };
cout << "1. SIMPLE............................................... " << maxScore( simple, 2, 0 ) << endl;
cout << "2. EASY................................................. " << maxScore( easy, 8, 0 ) << endl;
cout << "3. MEDIUM............................................... " << maxScore( medium, 25, 0 ) << endl;
cout << "4. MEDIUM HARD.......................................... " << maxScore( mediumhard, 3, 0 ) << endl;
cout << "5. IMPOSSIBLE........................................... " << maxScore( impossible, 20, 0 ) << endl;
cout << "6. INSANE............................................... " << maxScore( insane, 40, 0 ) << endl;
return 0;
}
答案 0 :(得分:0)
/* + 2 through the array */
int jumping( int track[], int size, int location ){
static int score = 0;
if ( track[ location ] == 1 ){
score++;
}
else if ( track[ location ] == -1 ){
score--;
}
/*** Need to assign recursion to score; also else condition has no return; suggest replacing with the following ***
if (( location == size - 1 ) || ( track[ location + 1 ] == 0 )){
return score;
}
else{
jumping( track, size, location + 2 );
}*/
/*** Also, you need to use size - 2 to prevent overrun ***/
if (( location < size - 2 ) && ( track[ location + 1 ] != 0 ))
score += jumping( track, size, location + 2 );
return score;
}
/* Run through the array by 1 */
int stepping( int track[], int size, int location ){
static int score = 0;
if ( track[ location ] == 1 ){
score++;
}
else if ( track[ location ] == -1 ){
score--;
}
/*** Need to assign recursion to score; also else condition has no return; suggest replacing with the following ***
if ( location == size - 1 ){
return score;
}
else{
stepping( track, size, location++ );
}*/
if (location < size - 1)
score += stepping( track, size, location++ );
return score;
}
/* Is to calculate the maxium possible score for any given track, with any arrangement using recursion. */
int maxScore( int track[], int size, , int location ){
int score = 0; //Keep track of score
int step = stepping( track, size, location );//score by stepping
int jump = jumping( track, size, location );//score by jumping
/* If step is higher or jump and replace according to the highest */
if ( step > jump ){
score = step;
//*** See Later *** return score;
}
else if ( jump > step ){
score = jump;
//*** See Later *** return score;
}
else /*** Don't need the if, as it is the only remaining condition *** if ( jump == step ) */{
//*** Score is not set here, is this intentional ***
//*** See Later *** return score;
}
return score;
}
答案 1 :(得分:0)
我发现了3个问题:
location >= size -2
,而不是location == size-1
,因为您的跳跃步数为2 return jumping(track, size, location + 2);
stepping( track, size, ++location );
而不是stepping( track, size, location++ );
Ideone:http://ideone.com/M0C68r