The second question: the question of the Progress

Source: Internet
Author: User

The second question: the question of the Progress

(1) A frog can jump to level 1 or Level 2 at a time. Find the total number of hops that the frog jumps to an n-level step. (2) A frog can jump to level 1 or Level 2 at a time ...... It can also jump to N levels. How many jumping methods does the frog jump to an n-level step?

# Include <stdio. h> # include <stdlib. h> int main (int ac, int * AV []) {int num_steps, result; printf ("Please input the num of steps \ n "); scanf ("% d", & num_steps); Result = 0; Result + = cal_steps (num_steps); printf ("there are % d ways for the frog to pass % d steps. \ n ", result, num_steps);} int cal_steps (int n) {int I, j, * A; A = (int *) malloc (N * sizeof (INT); a [0] = 1; A [1] = 2; for (I = 2; I <n; I ++) {for (j = 0; j <I; j ++) A [I] + = A [J]; A [I] + = 1 ;} return a [n-1];} int cal_steps_two (int n) {int I, pre, result, TMP; If (n = 1) return 1; if (n = 2) return 2; If (n> 2) {pre = 1; Result = 2; for (I = 2; I <n; I ++) {TMP = result; result = Result + pre; Pre = TMP;} return result;} int cal_steps_recursive (int n) {int steps; Steps = 0; If (n = 0) | (n <0) {return 0;} If (n = 1) return 1; if (n = 2) return 2; If (n> 2) {steps + = cal_steps (n-1) + cal_steps (n-2);} return steps ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.