20160329--For/while/do while/goto/recursion

Source: Internet
Author: User

Any algorithm is done using for/while/do while/goto/recursion .

The logic ability of the algorithm is enhanced by using recursion to rewrite any loop.


Converts an integer to a string, a binary lookup, and a data separation.

Conversion of integer Strings:

#include  <stdio.h> #include  <stdlib.h>int tonum (char * str) {     char * istr = str;    int num = 0;     while (*str !=  ')     {         if  (*STR) < ' 0 ' | | (*STR) > ' 9 ')         {             return -1;         }          *str++;          num++     }     printf ("%d",  num);      int lastres = 0;     for  (int i = 0;  i < num; i++) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;  lastres *= 10;         int wei  = istr[i] - 48;         lastres +=  wei;         printf ("\n wei = %d",  wei) ;      }     return lastres;} Void tostr (INT&NBSP;NUM,&NBSP;CHAR&NBSP;*&NBSP;STR) {    int wei = 0;     for (int inum = num; inum; inum/=10)     {         wei++;    }         for (int i = wei - 1; num; num/=10, i--)      {        str[i] = num % 10 +  48;    }}void main () {&nbsP;&NBSP;&NBSP;&NBSP;INT&NBSP;NUM&NBSP;=&NBSP;1234567;&NBSP;&NBSP;&NBSP;&NBSP;CHAR&NBSP;STR[10]&NBSP;=&NBSP;{0} ;     tostr (NUM,&NBSP;STR);     printf ("%s",  str);         getchar ();}


Recursion:

#define _CRT_SECURE_NO_WARNINGS//Close security check # include <stdio.h> #include <stdlib.h>double go (int n) {if (n = = 1    ) {return 1.0;    } else if (n = = 2) {return 2.0;    } else {return Go (n-1) +go (n-2); }}


Using While,do while, for, recursion, write out a binary lookup algorithm respectively.

Recursively determine if an array is decremented

The integer is converted to a string using the for, while, does while, Goto, recursively.

Recursive implementation 10-in-turn 8-binary

Recursively implements a string-to-integer (contains plus or minus), and the string is interchanged with double.








































20160329--For/while/do while/goto/recursion

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.