/*
Problem 1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
3 또는 5의 배수 인 10 이하의 모든 자연수를 나열하면 3, 5, 6 및 9가 됩니다. 이러한 배수의 합은 23입니다.
1000 이하의 3 또는 5의 모든 배수의 합계를 찾습니다.
*/
#include <iostream>
#define MAX 1000
using namespace std;
void setarr(int *org);
void setmul3(int *org, int *multiple3);
void setmul5(int *org, int *multiple5);
void calres(int *multiple3, int *multiple5, int *res);
void main()
{
int result = 0;
int org[MAX];
int multiple3[MAX];
int multiple5[MAX];
int res[MAX];
setarr(org); // 기본 숫자 배열 0, 1, 2, 3 ... 으로 초기화
setmul3(org, multiple3);
setmul5(org, multiple5);
calres(multiple3, multiple5, res);
for (int i = 1; i < MAX; i++) // 결과가 입력되어있는 배열의 수를 더하는 부분
{
result += res[i];
}
cout << result; // 결과값 출력
}
void setarr(int *org)
{
for (int i = 0; i < MAX; i++)
{
org[i] = i;
}
}
void setmul3(int *org, int *multiple3) // multiple3 배열에 3의 배수만 입력 ( 예) 0, 0, 3, 0, 0, 6, ...)
{
int res;
for (int i = 1; i < MAX; i++)
{
res = org[i] % 3;
if (res == 0)
{
multiple3[i] = org[i];
}
else
{
multiple3[i] = 0;
}
}
}
void setmul5(int *org, int *multiple5) // multiple5 배열에 5의 배수만 입력 ( 예) 0, 0, 0, 0, 5, 0, ...)
{
int res;
for (int i = 1; i < MAX; i++)
{
res = org[i] % 5;
if (res == 0)
{
multiple5[i] = org[i];
}
else
{
multiple5[i] = 0;
}
}
}
void calres(int *multiple3, int *multiple5, int *res) // 공통된 배수를 얻어내는 함수 ( 예) 0, 0, 3, 0, 5, 6, 0, 0, 9, ... )0, 9, ... )
{
int arr[MAX];
for (int i = 1; i < MAX; i++)
{
arr[i] = multiple3[i] || multiple5[i];
if (arr[i] == 1)
{
res[i] = i;
}
else
{
res[i] = 0;
}
cout << res[i] << endl;
}
}
Euler Project 문제 2번 C++ (0) | 2017.01.21 |
---|---|
C++ 객체/객체 포인터로 클래스의 함수 호출하기 (0) | 2016.12.21 |
C++ 클래스 관련 에러 발생 함수 수정하기 (0) | 2016.12.21 |
C++ 별 출력 예제 (0) | 2016.12.14 |
C++ 배열의 덧셈을 연산하는 코드 (0) | 2016.12.14 |