Description defines a recursive function sumint sum (int n); function declaration, return 12+22+32+......+n2 and note: Sum is the value of the recursive function input positive integer n output12+22+32+......+n2 and sample Input5sample Output55
/* Copyright (c) 2014, Yantai University School of Computer * All rights reserved. * File name: Test.cpp * Chen Dani * Completion Date: June 1, 2015 * version number: v1.0 * * #include <iostream> #include <cmath>using namespace s Td;int sum (int n); function declaration, 12+22+32+......+n2 and int main () { int n,s; cin>>n; s= sum (n); function call cout<<s<<endl; return 0;} int sum (int n) { int i,m,w=0; for (i=1;i<=n;i++) { m=i*i; w=w+m; } Return w;}
Experience: This is a simple recursive function, if encountered complex recursive function, before writing can find the law, written in the form of recursion, it is better. Keep trying!
C + + brush problem--2830: Recursive seeking 1*1+2*2+3*3+......+n*n