1. 快排三數取中spa
int Mid_of_Three(int a[],int left,int right) { int mid=left + (right-left)/2; if(a[right] < a[left]) Swap(a,left,right); if(a[right] < a[mid]) Swap(a,mid,right); if(a[left] < a[mid]) Swap(a,left,mid); return left; }
2. 兩次比較返回三數中值blog
int Mid_of_Three(int a,int b,int c) { if( (a-b)*(a-c) < 0 ) return a; if( (b-a)*(b-c) < 0 ) return b; else return c; }
3. 二叉樹高度class
int Cal_Height(point tree) // 版本1 { int n_lc=0, n_rc=0; if(!tree) return 0; else{ n_lc = cal_height(tree->lc); n_rc = cal_height(tree->rc); // return (n_lc > n_rc) ? (n_lc+1):(n_rc+1); //兩個都可 +1 是表明當前根節點 return 1 + max(cal_height(tree->lc), cal_height(tree->rc)); } } int Cal_Height(point tree) // 版本2 { if(!tree) return 0; else return 1 + max(cal_height(tree->lc), cal_height(tree->rc)); }
4. 高效輸出64位長整型二叉樹
void PrintInt64(long long a) { if (a<=100000000) printf("%d/n", a); else { printf("%d", a/100000000); printf("%08d/n", a%100000000); } }
5. a和b兩個數 + - * /運算,和/差的平均di