项目欧拉问题1是:找到低于1000的3或5的所有倍数的总和
这是我的程序,使用两个简单的函数计算出3的所有倍数和5的所有倍数之和,然后将它们相加:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
int threeSum( void );
int fiveSum( void );
int main( int argc, char** argv )
{
cout << "\n The sum of all the natural numbers below 1000 that are multiples of 3 or 5 = \n" << endl;
cout << threeSum() + fiveSum() << endl << endl;
system( "PAUSE" );
}
int threeSum( void )
{
int sumSoFar = 0;
for ( int i = 1 ; i < 1000 ; i++ )
{
if ( i % 3 == 0 )
sumSoFar = sumSoFar + i;
}
return sumSoFar;
}
int fiveSum( void )
{
int sumSoFar = 0;
for ( int i = 1 ; i < 1000 ; i++ )
{
if ( i % 5 == 0 )
sumSoFar = sumSoFar + i;
}
return sumSoFar;
}
Run Code Online (Sandbox Code Playgroud)
这产生266333了答案.这是正确的,还是我做错了,因为网站检查员说这是错误的答案!
| 归档时间: |
|
| 查看次数: |
7892 次 |
| 最近记录: |