Recursive Algorithm (simple) array with the array and without: eg1: Fibonacci series: the first term is 1, the second term is 1, the latter is equal to the sum of the first two the 10 num ask; eg2: monkey peaches were problems

**

eg1: number of columns evaluated

**
recursive algorithm than an array of (simple)
new value overwrites the old value

#include <stdio.h>
//正推
int main(int argc, const char * argv[]) {
    // insert code here...
    int a1 = 1,a2 = 1,t;
    printf("%d = %d\n",1,a1);
    printf("%d = %d\n",2,a1);
    for (int i = 1; i <= 8; i++) {
        t = a1 + a2;//第三个
        a1 = a2;
        a2 = t;
        
        printf("%d = %d\n",i+2,t);
    }
    return 0;
}

After using an array, the difference is, every number have been saved up

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int arr[10]={[0]=1,[1]=1};
    printf("%d\n",arr[0]);
    printf("%d\n",arr[1]);
   for (int i = 2; i < 10; i++) {
        arr[i] = arr[i-1]+arr[i-2];
        printf("%d\n",arr[i]);
    }
    return 0;
}

**

eg2: Monkey eating peach

**
1 than an array. Note change in value.

#include <stdio.h>
//反推
int main(int argc, const char * argv[]) {
    int day = 10,a = 1,t;
    while (day > 0) {
       //找到一个通项之后再进行循环
        t = a+2;//化简过后的等式
        printf("%d = %d\n",day,a);
        a = 2*(t-1);
        day--;
        
    }
    
    return 0;
}

There are an array of 2

#include <stdio.h>
//反推
int main(int argc, const char * argv[]) {
    int arr[10]= {[9]=1};
   
    for (int i = 8; i >= 0; i--) {
        arr[i] = 2*arr[i+1]+2;
        
    }
    printf("%d",arr[0]);
    return 0;
}

Summary
lay the draft
sort out ideas
to see positive solutions easy or reverse thrust
becomes constant and determine the value of good
words than an array of values in which the cycle will change Note
Note assign an initial value of an array of words

Published 18 original articles · won praise 0 · Views 202

Guess you like

Origin blog.csdn.net/weixin_46456339/article/details/105241821