﻿ 我无法理解这个程序是做什么的,它是一个递归的程序 - 代码日志

#### 我无法理解这个程序是做什么的,它是一个递归的程序

``````#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

#define SIZE 10

int whatIsThis(const int b[], size_t p);

int main(void) {
int x;
int a[SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

x = whatIsThis(a, SIZE);

printf("result is %d\n", x);

_sleep(1000 * 100);
}

int whatIsThis(const int b[], size_t p) {
if (1 == p) {
return b[0];
} else {
return b[p - 1] + whatIsThis(b, p - 1);
}
}
``````

What is this?

``````long long int whatIsThis( const int a[], size_t n )
{
return n == 0 ? 0 : a[n-1] + whatIsThis( a, n - 1 );
}
``````

``````long long int whatIsThis( const int a[], size_t n )
{
if ( n == 0 )
{
return 0;
}
else
{
return a[n-1] + whatIsThis( a, n - 1 );
}
}
``````

``````int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
``````

``````int a[10] = { { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 10 };
``````

``````int a[10] = { a1[9], 10 };
``````

``````int a1[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9  };
``````

``````int a1[9] = { a2[8], 9 };
``````

``````int a2[8] = { 1, 2, 3, 4, 5, 6, 7, 8  };
``````

``````int a8[2] = { a9[1], 2 };
``````

``````int a9[1] = { 1 };
``````

``````int a[10] = { { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 10 };
``````

``````int a[10] = { a1[9], 10 };
``````

``````int a1[9] = { 1, 2, 3, 4, 5, 6, 7, 8 }, 9  };
``````

``````int a1[9] = { a2[8], 9 };
``````