编程之美
前段日子又看了编程之美,后来闲着无聊学python去了,想着书将要送人,还是先总结一下,不然怕又要忘了,呵呵。
主要看的章节是第二、第三章,因为对数独有特别的感情,所以还顺带看了一四章关于数独的那两节。
1. 对于一个字节的无符号整形变量,求其二进制表示中“1”的个数。
最简单的莫过于用/跟%一个位一个位地测试:
int count( unsigned char n ){int num = 0;while( n ){if ( n%2 == 1 )num++;n >>= 1;}return num;}
int count( unsigned char n ){int num = 0;while( n ){n &= (n-1);num++;}return num;}
//N!的十进制表示末尾有多少个0int decCountLastZero( int n ){int ret = 0;while( n ){n /= 5;ret += n;}}//N!的二进制表示末尾有多少个0int binCountLastZero( int n ){int ret = 0;while( n ){n >>= 1;ret += n;}return ret;}
//假设以int来做IDtypedef int ID;ID find( ID* ids, int N ){ID candidate;int nTimes;//对所有ID进行一次遍历,nTimes用于标识两个不相等的ID号。for ( int i=nTimes=0; i<N; i++ ){if( nTimes == 0 ){candidate = ids[i];nTimes = 1;}else{if( candidate == ids[i] )nTimes ++;elsenTimes --;}}return candidate;}
typedef int ID;struct Record{ID rId;int count;Record( int c = 0 ){ count = c; }};void printTop3ID( ID* ids, int n ){Record rec[4];int i=0;while( i<n ){for( int j=0; j<4; j++ ){while( rec[j].count == 0 ){ID cand = ids[i++];int k=0;for( k=0; k<4; k++ ){if( rec[k].count>0 && rec[k].rId == cand ){rec[k].count++;break;}}if( k == 4 ){rec[j].count ++;rec[j].rId = cand;}}}for( int j=0; j<4; j++ )rec[j].count --;}for( int j=0; j<4; j++ ){if( rec[j].count > 0 )cout<<rec[j].rId<<endl;}}
int gcd1( int x, int y ){assert( x>0 && y>0 );while( y ){int t = x;x = y;y = t%y;}return x;}
int gcd2( int x, int y ){assert( x>0 && y>0 );while( x!=y ){if( x>y )x -= y;elsey -= x;}return x;}
int gcd3( int x, int y ){assert( x>0 && y>0 );int base = 1;while( x!=y ){if( x&0x1 == 0 ){if( y&0x1 == 0 ){base <<= 1;x >>= 1;y >>= 1;}elsex >>= 1;}else{if( y&0x1 == 0 )y >>= 1;else{if( x>y )x -= y;elsey -= x;}}}return base*x;}
bool isSubRotate( char* s1, char* s2 ){if( strlen(s1) < strlen(s2) )return false;int len = strlen(s1);char* s3 = new char[len*2+1];strncpy( s3, s1, len );strncpy( s3+len, s1, len );bool res = !(strstr(s3, s2) == NULL );delete s3;return res;}
char* my_strstr( const char* str1, const char* str2 ){const char* buf, *sub;if( !*str2 )return const_cast<char*>(str1);while( *str1 ){buf = str1;sub = str2;do{if( !*buf )return const_cast<char*>(str1);}while( *buf++ == *sub++ );str1 ++;}return NULL;}
int strdist( const char* strA, const char* strB ){int lenA = strlen(strA), lenB = strlen(strB);int** result = new int*[lenA+1];for( int i=0; i<lenA+1; i++ )result[i] = new int[lenB+1];//初始化边界for( int i=0; i<=lenA; i++ )result[i][lenB] = lenA-i;for( int i=0; i<=lenB; i++ )result[lenA][i] = lenB-i;for( int i=lenA-1; i>=0; i-- )for( int j=lenB-1; j>=0; j-- ){if( strA[i] == strB[j] )result[i][j] = result[i+1][j+1];elseresult[i][j] = minOfThree( result[i+1][j+1],result[i][j+1],result[i+1][j] )+1;}int ret = result[0][0];for( int i=0; i<lenA+1; i++ )delete[] result[i];delete[] result;return ret;}
template<typename T>struct Node{T value;Node *left, *right;Node( T v, Node* l=NULL, Node* r=NULL ):value(v), left(l), right(r){}};template<typename n>Node<T>* rebuild( T* pPreOrder, T* pInOrder, int len ){//pPreOrder是前序遍历的数组//pInOrder是后序遍历的数组//len是数组的长度,其实就是树结点的个数if( pPreOrder==NULL || pInOrder==NULL || len<1 )return NULL;Node<T>* subroot = new Node<T>(pPreOrder[0]);if( len == 1 )return subroot;//如果len=1,该树只有一个叶子结点T* rightInOrder=pInOrder;while( *rightInOrder != *pPreOrder )rightInOrder ++;int leftLen = rightInOrder-pInOrder;int rightLen = len-leftLen-1;subroot->left = rebuild( pPreOrder+1, pInOrder, leftLen );subroot->right = rebuild( pPreOrder+1+leftLen, rightInOrder+1, rightLen );return subroot;}
LinkList* IsCyclicLinkList( LinkList* pHead ){//pCur走一步,pStart走两步,pCycle指向pCur和pStart重合的地方,如果有的话LinkList* pCur, *pStart, *pCycle;pCur = pStart = pHead;pCycle = NULL;while( pCur != NULL && pStart != NULL ){if( pStart->next == NULL )return NULL;pStart = pStart->next->next;pCur = pCur->next;if( pStart == pCur ){pCycle = pStart;break;}}//测试是否有环if( pCycle != NULL )//有环,接下来将找环的入口点{//pCur从头找起,pStart则从重合的地方开始,如果pStart和pCur遇上了,说明pCur就是环的入口了//不然环的入口就是pCyclepCur = pHead;while( pCur != pCycle ){pStart = pCycle;while( pStart != pCur && pStart != pCycle )pStart = pStart->next;if( pStart == pCur )return pCur;pCur = pCur->next;}return pCycle;}return NULL;}