用递归求整个数组之和

来源:互联网 发布:js选择时间 编辑:程序博客网 时间:2024/06/04 18:40

递归代码如下:

// getSumOfArray.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>using namespace std;int sumOfArray(int a[],int len){if (len<=0){return 0;}return sumOfArray(a,len-1)+a[len-1];}int _tmain(int argc, _TCHAR* argv[]){int a[]={1,2,3};cout<<sumOfArray(a,3);system("pause");return 0;}


 

原创粉丝点击