求一个数的除数
Finding divisors of a number
本文关键字:一个 更新时间:2023-10-16
我试图编写一个程序来查找用户给出的数字的除数。我的程序正在编译,但它不能正常工作。
#include <iostream>
using namespace std ;
int main () {
int a,x,y,z,p ;
int d [z] ;
y=0 ;
cout << "Please give me the number you want me to check for dividors" << endl ;
cin >> a ;
for (x=2;x<a;x++){
if(a%x==0)
d[y]=x ;
y++ ;
}
if (y==0){
cout << a <<" has no dividors except itself and 1 and therefore, " << a <<" is a prime" << endl ;
}
else
cout << a <<" has " << y <<" dividors except itself and 1 and this dividors are :" << endl ;
for (p=0;p<=y;p++){
cout << d[p] <<endl ;
}
return 0 ;
}
我想加上这句话作为评论,但是我还没有足够的声誉。
无论如何,我认为问题在于你的最后一个for循环。当y
是数组d[]
中的除数时,你迭代到p <= y
。d[y]
是越界的,循环应该只迭代p < y
。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 从链接列表c++中删除一个项目
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '