C++ C++;控制台程序在没有任何错误的情况下崩溃
我试图解决Project Euler的问题5,但程序崩溃,我没有得到任何错误:C++ C++;控制台程序在没有任何错误的情况下崩溃,c++,C++,我试图解决Project Euler的问题5,但程序崩溃,我没有得到任何错误: #include <stdio.h> #include <iostream> using namespace std; /* Problem 5: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is
#include <stdio.h>
#include <iostream>
using namespace std;
/* Problem 5:
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
*/
int selle(int number) {
int c = 0;
for (int i = 0; i <= 20; i++)
if (number % i == 0)
c++;
return c;
}
int problem(int number) {
while (number > 0) {
if (selle(number) == 20)
return number;
number++;
}
return 404;
}
int main() {
long long number = 2;
cout << problem(number);
system("PAUSE");
return 0;
}
#包括
#包括
使用名称空间std;
/*问题5:
2520是最小的数字,可以被1到10之间的每一个数字除,没有任何余数。
能被1到20的所有数整除的最小正数是多少?
*/
整数卖方(整数编号){
int c=0;
对于(int i=0;i 0){
if(卖方(数量)==20)
返回号码;
数字++;
}
返回404;
}
int main(){
长数=2;
coutfor(int i=0;i问题是您在某个点执行编号%0
。与除零一样,也不允许按零模运算。如果模运算中的第二个操作数为0,则会导致未定义的行为()
顺便说一句,您可以从long long number=20;
开始,然后执行20的增量(number+=20;
),因为在这两者之间找不到任何匹配项。我想您应该使用根据欧几里得最大公约数(GCD)定义的最小公倍数(LCM)函数.FIY,在Windows中,程序会生成一个错误框“停止工作”。在旧时代,它鼓励用户告诉比尔·盖茨。显然,这给他带来了太多的工作,因为现在它说Windows正在检查解决方案(人工智能,耶!),然后更改以允许我调试或关闭程序。French的gcc告诉我点flottant的异常情况!很高兴我能提供帮助。如果这回答了你的问题,你应该投赞成票并接受答案。
for (int i = 0; i <= 20; i++)
if (number % i == 0)
c++;