1. Tower Problems
2 Joseph's ring
3. Find the "Fibonacci sequence:, 13, 21..." function: recursion and non-recursion
4. Determine whether a number is a power of 2:
5. Calculate the number num of 1 of the binary number of an integer.
6. Enter an integer array to adjust the order of numbers in the array so that all odd numbers are in the first half of the array, and all even numbers are in the second half of the array. The time complexity is O (n ). ---- Algorithms that require time complexity, such
========================================================== ========================================================== ==============
1. Tower Problems
//First, move n-1 disks on X to Y through Z.
Second, move the bottom disk on X to Z.
// Third, because n-1 disks are all on Y, you can repeat the preceding steps as X. Data is saved in recursion.
void moveNext( char pre, char next ){ cout<<pre<<"-->"<<next<<endl;}void hanio(int n,char XBlock,char YBlock,char ZBlock){ if(n==1) { moveNext(XBlock,ZBlock); } else { hanio(n-1,XBlock,ZBlock,YBlock); moveNext(XBlock,ZBlock); hanio(n-1,YBlock,XBlock,ZBlock); }}
2 Joseph's ring
First, create a loop linked list connected at the beginning and end, define a calculator for the for loop, and change the connection relationship of the linked list at m.
Until the last node is left, pre = purnode.
typedef struct node { int num; struct node *next;}LinkNode;
LinkNode * create_link (int num) {LinkNode * p, * current, * head; // p is used for loop, and current is used to record the current position. Head (number 1) is used to return a linked list p = (LinkNode *) malloc (sizeof (LinkNode); p-> num = 1; p-> next = NULL; head = p; current = p; for (int I = 2; I <= num; I ++) {// generate a new node --- establish a connection ---- transfer to the next p = (LinkNode *) malloc (sizeof (LinkNode); p-> num = I; p-> next = NULL; current-> next = p; current = p;} current-> next = head; // the rail of the linked list points to head, create a loop (loop list ). Return head;} void make_play (LinkNode * link, int mcode) {assert (link! = NULL & mcode! = 0); LinkNode * pLink = link, * pPre = NULL; // error: You must assign a value to the pre before using it. LinkNode * pDelte; // delete node --- free mark node) cout <"dear, the game has started! "<Endl; while (pLink! = PPre) // leave only one element at the end. {For (int n = 1; n <mcode; n ++) {pPre = pLink; pLink = pLink-> next;} pDelte = pLink; cout <"the column is" <pDelte-> num <endl; pPre-> next = pLink-> next; pLink = pPre-> next; free (pDelte);} cout <"the last column is:" <pLink-> num <endl ;}
3. Find the "Fibonacci sequence:, 13, 21..." function: recursion and non-recursion
It mainly refers to a Conversion Relationship Between the initialization exit condition) and the recursive relationship of the cyclic condition transfer.
Int fib (int n) {/* if (n = 1 | n = 2) {return 1;} else {return fib (n-1) + fib (n-2);} */if (n <= 2) {return 1;} int valN1, valN2, sumN; // is it possible to write static form valN1 = 1; valN2 = 1; // sum, n1, n2 move back position for (int I = 1; I <= n-2; I ++) // calculated count {sumN = valN1 + valN2; valN2 = valN1; valN1 = sumN;} return sumN ;}
4 determine whether a number is a power of 2: 0 = (a [I] & (a [I]-1)
int find2power( int a[] ,int len){ int count=0; for(int i=0;i<len;i++) { if(0==(a[i]&(a[i]-1))) { count++; } } cout<<count<<endl; return count;}
5. Calculate the number num of 1 of the binary number of an integer.
Count the number of 1 of the binary number of an integer: num & 1 (whether the rightmost value is 1 at a time)
Int num1OfBinery (int num) {int count = 0; while (num) {if (num & 1) {count ++;} num >>=1; // move to the right, divided by 2} return count ;}
6. Enter an integer array to adjust the order of numbers in the array so that all odd numbers are in the first half of the array, and all even numbers are in the second half of the array. The time complexity is O (n ).
---- Algorithms that require time complexity On): Add a mark to calculate the odd number of elements currently encountered to mark the position where the next odd number should be inserted ), in case of odd numbers --- swapi, mark)
Void swapElement (int arr [], int I, int mark) {int tmp; tmp = arr [I]; arr [I] = arr [mark]; arr [mark] = tmp;} void printArrray (int arr [], int num) {cout <"the output array is:" <endl; for (int I = 0; I <num; I ++) {cout <arr [I] <endl ;}// two variables: One I traversal array, the other flag requires several odd numbers that match the condition, which are in the first few places) // 1 locate the first one and determine to move 2 cycles void partition (int arr [], int num) {int I = 0, mark = 0; while (I <num & (arr [I] & 1 = 0) // locate the first odd number: = while + if (break) {I ++;} if (I = num) // all return; swapElement (arr, I, mark); I ++; mark ++; while (I <num) {if (arr [I] & 1) = 1) {swapElement (arr, I, mark); mark ++; printArrray (arr, num);} I ++;} printArrray (arr, num );}
========================================================== ==========================================
Test code:
/* Int n; printf ("Enter the number of blocks to be moved:"); scanf ("% d", & n); hanio (n, 'x ', 'y', 'z'); */int num = 7; // cout <fib (num) <endl; LinkNode * head = create_link (num ); make_play (head, 5); int arr [] = {1, 2, 3, 4, 5, 7, 8, 16, 50}; int len = sizeof (arr)/sizeof (int ); find2power (arr, len);/* int num = 7; cout <num1OfBinery (num) <endl; * // * int arr [] = {1, 2, 4, 6, 7, 5, 8, 9}; int n = sizeof (arr)/sizeof (int); partition (arr, n );*/