Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Seek the sum of shaping
Seek the sum of shaping
/* Because this problem is not to be solved directly, but to be diversified and not restricted by environmental conditions.

I use your questions to practice my new writing. You need a compiler gcc > to support the latest C++ 1 1 standard. 4.6

Compile and run.

*/

# include & ltiostream & gt

# include & lt algorithm & gt

# include & ltnumeric & gt

Use namespace std

int main(){

//randomly generate the number of 10 (VC >;; In 2008, GCC & gt3.5)

int a[ 10];

int count = 0;

srand(time(NULL));

generate_n(a, 10,[]{ return rand()% 20; });

for_each(a,a+ 10,[](int I){ cout & lt; & lt me<& lt''; });

cout & lt& ltendl

//method 1 range traversal (even the latest Visual C++20 12 is not supported, GCC >;; 4.6)

int sum = 0;

for(int & amp; I:a)sum+= I;

Cout & lt& lt "Total 1: "<

//Method 2 High-order reduce(VC & gt;; 6.0/98)

Cout & lt& lt "Sum 2:"

//Method 3 Recursion (VC & gt20 10)

Function & ltint(int*, int) > summfunc = [&; sumfunc](int *a,int l)-& gt; int { return l & gt 1? (*a)+sumfunc(a+ 1,l- 1):* a; };

Cout & lt& lt "3 in total:"

//Method 4 Local Sum Set (VC >;; 6.0)

int b[ 10];

partial_sum(a,a+ 10,b);

Cout & lt& lt "Four in total:"

Returns 0;

}

==========

19 16 9 8 6 19 12 19 9 17

Total 1: 134

Total 2: 134

Total 3: 134

Total 4: 134