我正在为一个Project Euler难题编写这段代码来练习编码,我遇到的问题是我认为是一些语法错误。我在这里做错了什么?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int smallestMultiple = 10000;
int sum = 1;
for (int i = 100, i < smallestMultiple, i+2)
{
for (int j = 20, j >=10, j--)
{
sum = sum + (i % j);
}
if (sum == 1)
{
smallestMultiple = i;
}
else
{
sum = 1;
}
}
cout<< "The smallest number easily divisible by the numbers 1 to 20 is " << smallestMultiple << "." << endl;
}
我在尝试编译此代码时收到以下错误。我错过了什么类型的语法?
smallMultiple.cpp:6: error: expected ‘;’ before ‘int’
smallMultiple.cpp: In function ‘int main()’:
smallMultiple.cpp:12: error: expected initializer before ‘<’ token
smallMultiple.cpp:32: error: expected primary-expression at end of input
smallMultiple.cpp:32: error: expected ‘;’ at end of input
smallMultiple.cpp:32: error: expected primary-expression at end of input
smallMultiple.cpp:32: error: expected ‘)’ at end of input
smallMultiple.cpp:32: error: expected statement at end of input
smallMultiple.cpp:32: error: expected ‘}’ at end of input
答案 0 :(得分:0)
这里有几个问题:
for
循环的部分应使用分号(;
)分隔,而不是逗号(,
)for
循环中,i + 2
未存储在任何位置。假设您打算将2
添加到i
的值,则应使用i+=2
。因此,总之,您的for
循环应如下所示:
for (int i = 100; i < smallestMultiple; i+=2)
{
for (int j = 20; j >=10; j--)
{
// rest of the code
答案 1 :(得分:0)
用于的用法;为了区分三个表达式(初始化,条件和更新),因此你应该写:
for (int i = 100; i < smallestMultiple; i+=2)
而不是:
for (int i = 100, i < smallestMultiple, i+2)
如果你想将i从100迭代到最小的多个,增量为2