You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
417 B

#include<iostream>
using namespace std;
int SumArray(int a[], int n, int len)
{
if (n == 0) //基线条件
{
return 0;
}
else //递归条件
{
return a[len - n] + SumArray(a, n - 1, len);
}
}
int main()
{
int a[50], n;
cout<<"请输入数组中数的个数";
cin >> n;
cout<<"请向数组输入";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout <<"数组的和为:" ;
cout<<SumArray(a, n, n);
return 0;
}