Search questions and frog stair crawling Problems

Source: Internet
Author: User
The followingCodePut an app file. Checknumber (), frog_count1 (), and frog_count2 () areAlgorithmThree functions are implemented. Others are auxiliary functions for testing and testing. Do not use them, as shown below:
 
 
# Include "stdlib. H "# include" stdio. H "// fill the arrayvoid fillarray (int * PTR, int M, int N); // show the input arrayvoid testarray (int * PTR, int M, int N ); // check out if number is in the arraybool checknumber (const int * PTR, int num, int M, int N ); // frog can jump 1 or 2 steps one time // Param n is the steps need to jump, return the number of methodsint frog_count1 (int n); // frog can jump 1, 2... n steps one Time // Param n is the steps need to jump, return the number of methodsint frog_count2 (int n); int main () {/************************************** **************************************** ******************//******************** ***** First One: find the value from the array *******************************//* **************************************** **************************************** ***************/// Input arrayint m, n, Val; int * PTR = NULL; M = n = 0; printf ("input the row size of array: \ n "); scanf ("% d", & M); printf ("input the colum size of array: \ n"); scanf ("% d", & N ); if (M> 0 & n> 0) {PTR = (int *) malloc (M * n * sizeof (INT); If (PTR = NULL) {printf ("failed to allocate Mem! \ N "); Return 0;} fillarray (PTR, m, n);} testarray (PTR, m, n); printf (" input the number you want to check: \ n "); scanf (" % d ", & Val); // check if number is in the arraybool isin = checknumber (PTR, Val, m, n ); printf ("% d is % sin the array. ", Val, (ISIN? "": "Not ")); /*************************************** **************************************** **************//************************ * second one: frog jumping quesion *******************************//**** **************************************** **************************************** * *******/n = 0; int COUNT = 0; printf ("Please input that how many steps you want the frog to jump first time: \ n"); SC ANF ("% d", & N); If (n> = 0) Count = frog_count1 (N ); // 1 or 2 steps one timeelseprintf ("there are no steps below the frog, you shoshould know that \ n "); printf ("there are % d way % s to do this in case of 1 or 2 steps one time. \ n ", Count, Count = 1? "": "S"); printf ("Please input that how many steps you want the frog to jump second time: \ n"); scanf ("% d ", & N); If (n> = 0) Count = frog_count2 (n); // 1, 2, 3 ,.... n times one timeelseprintf ("there are no steps below the frog, you shoshould know that \ n "); printf ("there are % d way % s to do this in case of 1-% d steps one time. \ n ", Count, count <2? "": "S", n); Return 0;} void fillarray (int * PTR, int M, int N) {for (INT I = 0; I <m; I ++) {printf ("Please input the data for line % d, using space between the data: \ n", I); For (Int J = 0; j <n; j ++) scanf ("% d", PTR + I * n + J) ;}} void testarray (int * PTR, int M, int N) {for (INT I = 0; I <m; I ++) {for (Int J = 0; j <n; j ++) {printf ("% 3d ", * (PTR + I * n + J);} printf ("\ n");} bool checknumber (const int * PTR, int num, int M, int N) {int I, j, x1, x2, Y1, Y2; X1 = n-1; x2 = 0; Y1 = 0; y2 = m-1; // ensure the num is in the range of [x2-x1] By columeif (PTR [X1] = num) return true; while (PTR [X1]> num) {x1 --; if (PTR [X1] = num) return true;} If (PTR [N * (m-1) + x2] = num) return true; while (PTR [N * (m-1) + x2] <num) {X2 ++; If (PTR [N * (m-1) + x2] = num) return true;} // ensure the num is in the range of [y1-y2] By rowwhile (PTR [X1 + Y1 * n] <num) {y1 ++; if (PTR [X1 + Y1 * n] = num) return true;} while (PTR [X2 + y2 * n]> num) {Y2 --; if (PTR [X2 + y2 * n] = num) return true;} for (I = Y1; I <y2 + 1; I ++) {for (j = x2; j <X1 + 1; j ++) {If (PTR [I * n + J] = num) return true ;}} return false;} int frog_count1 (int n) {int ret; If (n = 0) return 0; If (n = 1) ret = 1; if (n = 2) ret = 2; If (n> 2) ret = frog_count1 (n-1) + frog_count1 (n-2); return ret ;} int frog_count2 (int n) {int ret = 1; if (n = 1) ret = 1; if (n> 1) {While (n> 1) {RET + = frog_count2 (n-1); n -- ;}} return ret ;}

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.