我需要帮助创建一个嵌套的for循环。这是用于“跳跃游戏”的,基本上有一个数组,里面有一些数字。每个数组位置的数字都有一定的次数可以跳到末尾。例如:[2 3 1 1 4 0],所以array[0] = 2,它可以跳到2或1位,array[1] = 3,可以跳到3,2或1位。例如:[3 2 1 0 4 0],所以你基本上永远不能到达数组中的位置。
我已经做了一段时间了,还是想不出循环的方法。readFromFile基本上只是创建数组大小。现在我有了尺寸,我需要检查我粘在上面的每个数字。
void readFromFile(int list[], int& size, ifstream& infile)
{
infile >> list[size];
while (!infile.eof()) // Loops until end of file is reached
{
size++;
infile >> list[size];
}
infile.close();
return;
}
bool getToLastIndex(int list[], int size)
{
for(i=0; i< size;i++)
{
if(list[i] == 0)
break;
j=i+list[i]; //Resetting it for a new jump or section on array?
i=j;
for(int j=0; j <= list[i]; j++)
{
list[j] == return true;
}
}
}我希望能够使用getToLastIndex(list,size);
if(getToLastIndex() == true)
{
cout << "Mario FTW" << endl; // Got to the end
}
else
cout << "This bridge has the workings of bowser all over it >:( " << endl; //Couldnt make it to the end作业是以马里奥为主题的。我没有选择输出
发布于 2019-04-25 05:25:54
尝试以下操作:
void readFromFile(int list[], int &size, ifstream& infile)
{
size = 0;
if(!infile.is_open())
return;
while(!infile.eof())
{
infile >> list[size++];
}
infile.close();
}
bool isTheLastReachable(int list[], int index, int size)
{
if(index < size)
{
if(index)
{
for(int i = 0; i < list[index]; ++i)
if(isTheLastReachable(list, index + i, size))
return true;
return false;
}
else
return false;
}
return true;
}
bool getToLastIndex(int list[], int size)
{
return isTheLastReachable(list, 0, size);
}https://stackoverflow.com/questions/55838557
复制相似问题