C++ 为什么此程序在此输入上显示运行时错误?
我从uva上的这个问题得到第三个输入的运行时错误。我不知道我的密码在哪里被破解C++ 为什么此程序在此输入上显示运行时错误?,c++,runtime-error,C++,Runtime Error,我从uva上的这个问题得到第三个输入的运行时错误。我不知道我的密码在哪里被破解 #包括 使用名称空间std; int main(){ 长整数步长,模,i=0,温度; cin>>步骤>>模块; 整数计数[mod+1]={0}; 计数[0]=1; 载体种子; 种子。推回(0); while(true) { 种子[i+1]=(种子[i]%mod+步长%mod)%mod; 温度=种子[i+1]; 如果(温度==0) { 打破 } 计数[温度]+=1; ++一,; } 布尔标志=真; 对于(int i=
#包括
使用名称空间std;
int main(){
长整数步长,模,i=0,温度;
cin>>步骤>>模块;
整数计数[mod+1]={0};
计数[0]=1;
载体种子;
种子。推回(0);
while(true)
{
种子[i+1]=(种子[i]%mod+步长%mod)%mod;
温度=种子[i+1];
如果(温度==0)
{
打破
}
计数[温度]+=1;
++一,;
}
布尔标志=真;
对于(int i=0;ii
增加,但由其索引的向量的大小不会增加。事实上,在第一次迭代中,seed
的大小为1,因此seed[0]
是唯一有效的索引。但您可以访问seed[i+1]
即seed[1]
。您的程序通过访问超出边界的索引来显示未定义的行为。int count[mod+1]={0}这是无效的C++。C++中的数组必须用常数表达式表示它们的大小,而不是运行时计算值,如“代码> MOD+ 1 < /代码>。您已经在程序中使用<代码>向量< /代码>,因此您应该在这里使用:<代码> STD::向量计数(MOD+ 1);< /C>。
#include <bits/stdc++.h>
using namespace std;
int main() {
long int step,mod,i=0,temp;
cin >> step >> mod;
int count[mod+1] = {0};
count[0] = 1;
vector<int> seed;
seed.push_back(0);
while(true)
{
seed[i+1] = (seed[i]%mod + step%mod)%mod;
temp = seed[i+1];
if(temp == 0)
{
break;
}
count[temp] += 1;
++i;
}
bool flag = true;
for(int i=0; i<mod; i++)
{
if(count[i] <= 0)
{
flag = 0;
break;
}
}
if(flag)
cout << "Numbers have been generated\n";
else
cout << "Wrong choice\n";
return 0;
}