C++ 用向量解约瑟夫问题
编辑:我似乎已经整理好了错误,至少,我已经更新了代码。然而,数学似乎仍然不可行。有什么想法吗 简而言之,我试图编写一个C++程序,它会提示用户在初始循环中的人数,然后告诉他们应该在哪个位置生存,如果<强> k<强>(被执行之前的人数)=3。 我得到了我认为正确的想法,但如果我输入k而不是1、2或5,则会出现错误“调试断言失败”和“表达式:向量擦除迭代器超出范围”C++ 用向量解约瑟夫问题,c++,vector,josephus,C++,Vector,Josephus,编辑:我似乎已经整理好了错误,至少,我已经更新了代码。然而,数学似乎仍然不可行。有什么想法吗 简而言之,我试图编写一个C++程序,它会提示用户在初始循环中的人数,然后告诉他们应该在哪个位置生存,如果 k(被执行之前的人数)=3。 我得到了我认为正确的想法,但如果我输入k而不是1、2或5,则会出现错误“调试断言失败”和“表达式:向量擦除迭代器超出范围” // ConsoleApplication2.cpp : Defines the entry point for the console appl
// ConsoleApplication2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int n;//size of the circle
vector <int> circle; //the circle itself
//ask for how many people are in the circle
cin >> n;
//fill the circle with 1,2,3...n
for (int idx = 0; idx < n; idx++)
{
circle.push_back (idx+1);
}
//cout << "The size of the circle is " << circle.size() << ".\nThe highest number is " << circle[n-1] << "."; //test to make sure numbers are being assigned properly to each vector element
for (int count = 0, idx = 0; circle.size() > 1; idx++,count++)
{
//if the position (idx) is greater than the size of the circle, go back to the beginning of the circle and start counting again
if (idx >= circle.size())
{
idx = 0;
}
//every time the counter reaches three, that person is executed
if (count == 3)
{
circle.erase (circle.begin()+(idx));
count = 0;
}
}
cout << "The place to stand to win the hand is position #" << circle.front() << ".\n";
return 0;
}
//ConsoleApplication2.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
int n;//圆的大小
向量圆;//圆本身
//询问这个圈子里有多少人
cin>>n;
//用1,2,3…n填充圆圈
for(intidx=0;idx //您只能检查if(idx>circle.size())
,然后继续调用circle.erase(circle.begin()+(idx));
。当idx==circle.size()
时,此调用不安全。if(idx>circle.size())
,然后继续调用circle.erase(circle.begin()+(idx))
。当idx==circle.size()
你只检查if(idx>circle.size())
然后继续调用circle.erase(circle.begin()+(idx));
。当idx==circle.size()
你只检查if(idx>circle.size())时,这个调用不安全
然后继续调用circle.erase(circle.begin()+(idx));
当idx==circle.size()
时,此调用不安全。@Pradhan已经为您的原始错误提供了解决方案(idx>=circle.size()
而不是idx>=circle.size()
)
对于结果仍然不正确的原因:
当你删除一个元素时,你必须调整你的索引来补偿它(减去1)。否则索引对应于向量中的下一个元素,所以你总是每四个人执行一次,而不是每三个人执行一次
以下是我的代码版本:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main(){
int n;
cin >> n;
//fill the circle with 1,2,3...n
vector <int> circle(n);
std::iota(std::begin(circle), std::end(circle),1);
int idx = -1;
while (circle.size() > 1) {
//advance by threee
idx = (idx + 3) % circle.size();
//execute Person
circle.erase(circle.begin() + (idx));
//readjust to compensate for missing element
--idx;
}
cout << "The place to stand to win the hand is position #" << circle.front() << ".\n";
}
@Pradhan已经为您提供了原始错误的解决方案(idx>=circle.size()
,而不是idx>=circle.size()
)
对于结果仍然不正确的原因:
当你删除一个元素时,你必须调整你的索引来补偿它(减去1)。否则索引对应于向量中的下一个元素,所以你总是每四个人执行一次,而不是每三个人执行一次
以下是我的代码版本:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main(){
int n;
cin >> n;
//fill the circle with 1,2,3...n
vector <int> circle(n);
std::iota(std::begin(circle), std::end(circle),1);
int idx = -1;
while (circle.size() > 1) {
//advance by threee
idx = (idx + 3) % circle.size();
//execute Person
circle.erase(circle.begin() + (idx));
//readjust to compensate for missing element
--idx;
}
cout << "The place to stand to win the hand is position #" << circle.front() << ".\n";
}
@Pradhan已经为您提供了原始错误的解决方案(idx>=circle.size()
,而不是idx>=circle.size()
)
对于结果仍然不正确的原因:
当你删除一个元素时,你必须调整你的索引来补偿它(减去1)。否则索引对应于向量中的下一个元素,所以你总是每四个人执行一次,而不是每三个人执行一次
以下是我的代码版本:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main(){
int n;
cin >> n;
//fill the circle with 1,2,3...n
vector <int> circle(n);
std::iota(std::begin(circle), std::end(circle),1);
int idx = -1;
while (circle.size() > 1) {
//advance by threee
idx = (idx + 3) % circle.size();
//execute Person
circle.erase(circle.begin() + (idx));
//readjust to compensate for missing element
--idx;
}
cout << "The place to stand to win the hand is position #" << circle.front() << ".\n";
}
@Pradhan已经为您提供了原始错误的解决方案(idx>=circle.size()
,而不是idx>=circle.size()
)
对于结果仍然不正确的原因:
当你删除一个元素时,你必须调整你的索引来补偿它(减去1)。否则索引对应于向量中的下一个元素,所以你总是每四个人执行一次,而不是每三个人执行一次
以下是我的代码版本:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main(){
int n;
cin >> n;
//fill the circle with 1,2,3...n
vector <int> circle(n);
std::iota(std::begin(circle), std::end(circle),1);
int idx = -1;
while (circle.size() > 1) {
//advance by threee
idx = (idx + 3) % circle.size();
//execute Person
circle.erase(circle.begin() + (idx));
//readjust to compensate for missing element
--idx;
}
cout << "The place to stand to win the hand is position #" << circle.front() << ".\n";
}
你真的需要把它清理干净。circle.begin()
单独一行的目的是什么?我同意。我对这有点陌生,所以我仍在努力弄清楚。你真的需要清理一下。circle.begin()的目的是什么
一行一行?我同意。我对这有点陌生,所以我仍在努力解决问题。你真的需要清理一下。循环.begin()的目的是什么
一行一行?我同意。我对这有点陌生,所以我仍在努力解决问题。你真的需要清理一下。循环.begin()的目的是什么一行本身?我同意。我对这有点陌生,所以我仍在尝试解决问题。嗯。这是因为圆圈。大小
从1开始,而向量本身从0开始?它的索引为零,所以要注意那些由一个错误隔开的错误。嗯。这是因为圆圈。大小从1开始,而向量本身从一开始吗t0?它是零索引的,所以要注意那些由一个错误关闭的。嗯。这是因为circle.size
从1开始,而向量本身从0开始吗?它是零索引的,所以要注意那些由一个错误关闭的。嗯。这是因为circle.size
从1开始,而向量本身从0开始吗?它是零索引的,所以要注意那些一个接一个的错误。