Ruby port solution for C ++

Is there a way to do this in C ++, especially in the range section.

answer = (0..999).select { |a| a%3 ==0 || a%5==0 } puts answer.inject { |sum, n| sum+n } 

I created my own C ++ solution, but used a more standard loop for the loop and wondered if there was a cooler way to do this?

+7
source share
4 answers

Template metaprogramming solution:

It is further assumed that the lower end of the range is 0.

 template <int N> struct sum { static const int value = sum<N-1>::value + (N % 3 == 0 || N % 5 == 0 ? N : 0); }; template <> struct sum<0> { static const int value = 0; }; int main(int argc, char** argv) { int n = sum<999>::value; return 0; } 

Below you can specify a range of numbers (for example, 0-999, 20-400). I am not a master of metaprogramming templates, so I could not come up with a cleaner solution (and I did it for my own benefit and practice).

 template <int N, int Upper, bool IsLast> struct sum_range_helper { static const int value = (N % 3 == 0 || N % 5 == 0 ? N : 0) + sum_range_helper<N + 1, Upper, N + 1 == Upper>::value; }; template <int N, int Upper> struct sum_range_helper<N, Upper, true> { static const int value = (N % 3 == 0 || N % 5 == 0 ? N : 0); }; template <int Lower, int Upper> struct sum_range { static const int value = sum_range_helper<Lower, Upper, Lower == Upper>::value; }; int main(int argc, char** argv) { int n = sum_range<0, 999>::value; return 0; } 
+10
source

Unverified code. Uses C ++ 0x function (lambda function and iota)

 vector<int> v(1000); //fill the vector iota(v.begin(),v.end(),0); v.erase(remove_if(v.begin(),v.end(),[](int a) { return !(a%3 && a%5); }),v.end()); int sum = accumulate(v.begin(),v.end(),0); 
+4
source

Equivalent C ++ program:

 #include <iostream> using namespace std; int main() { int sum = 0; for (int i=0; i <= 999; i++) { if (i%3 == 0 || i%5 == 0) sum += i; } cout << sum; return 0; } 
+2
source

Here's a cool version of C:

 #include <stdio.h> #define LAMBDA(body, ...) ({ int _(__VA_ARGS__) { return (body); }; _; }) int range(int* arr, int start, int end) { (*arr = start) < end && range(arr + 1, start + 1, end); } void select(int* arr, int count, int(*fn)(int)) { int i; for(i = 0; i < count; i++) if(!fn(arr[i])) arr[i] = 0; } int inject(int* arr, int count, int(*fn)(int,int)) { int acc = arr[0], i; for(i = 1; i < count; i++) acc = fn(acc, arr[i]); return acc; } int main() { int numbers[1000]; range(numbers, 1, 1000); select(numbers, 1000, LAMBDA(a % 3 == 0 || a % 5 == 0, a)); printf("%d\n", inject(numbers, 1000, LAMBDA(a + b, a, b))); } 

http://codepad.org/eUKFAvkc

+2
source

All Articles