From 8892f1b5980174279d808c7c8996d918ef0c14ba Mon Sep 17 00:00:00 2001 From: chensongliang <2018631796@qq.com> Date: Sat, 28 Oct 2023 16:47:40 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=92=E5=BD=92=E6=B1=82=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 递归求和.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 递归求和.cpp diff --git a/递归求和.cpp b/递归求和.cpp new file mode 100644 index 0000000..f73acf8 --- /dev/null +++ b/递归求和.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +int SumArray(int a[], int n, int x) { + if (n == 0) { + return 0; + } else { + return a[x - n] + SumArray(a, n - 1, x); + } +} + +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; +} \ No newline at end of file