原创 判斷一個點是否在一個三角形內(二維)

設有三角形abc和點p,需要判斷p是否在三角形abc內部 只需要用叉乘進行判斷,abXap, apXac,pbXpc是否都與abXac平行。 另外面積法也能判斷,判斷三角形abp,acp,bcp的面積和是否等於三角形abc的面積。其中,求

原创 查找字符串中最長的連續數字子串

#include <iostream> using namespace std; int max_num_str(char* pstr, char* output) { if ( *pstr == '\0') { *outpu

原创 static data members

在c++中,對於static data members,通過一個指針和通過一個對象來存取member的效果是一樣的,而且這是唯一一樣的situati

原创 字符存在

當有一個字符串str1,另一個字符串str2,要判斷str2中的字符是否在str1中存在,可以使用hash表判斷,O(m+n)。 也可以將每一個字符一次賦予一個素數, 從2開始,然後將這些素數相乘。然後遍歷第二個字符串,用素數去除前一次的

原创 將有序數組轉化爲二叉樹

using namespace std; struct node { int val; struct node* left; struct node* right; node(int v=0) { val = v; l

原创 內存分佈

struct test { int a; int b; int c; }; int main(int argc, char* argv[]) { test x; test y; printf("%x, %x, %x\n", &x.

原创 繼承的內存分佈和代價

class B { private: int val; char c1; char c2; char c3; }; class A1 { private: int val; char c1; }; class A2: pu

原创 排序N個比N^7小的數,要求的算法是O(n)

採用基數排序,用10個桶(vector),每一個桶用隊列表示(queue),分別代表0-9,然後依次從低位到高位開始將要排序的數倒入桶中,然後再順序取出來,直到排序完成。 若有5個數,12,4,5,130,28 第一次(個位): 0: 1

原创 不相關成員函數的調用

#include <iostream> using namespace std; class base { public:     base(): a(0){}     ~base(){};     void show()     {

原创 優先級倒掛

優先級倒掛是指一個低優先級任務持有一個被高優先級認爲所需要的共享資源。這樣高優先級任務因資源缺乏一直處於受阻狀態,一直到低優先級任務釋放資源爲止。這

原创 inline關鍵字

inline int add(int a,int b) 1.參數直接替換形參, int a = 1; int b = 2; int c = add(a, b); 2.直接才編譯過程中,能夠判斷出結果的,直接給出結果, int c = ad

原创 查找字符串中第一個出現一次的字符

#include <iostream> using namespace std; // look up the first character in the string. // using hash table to reduce

原创 如何維護一箇中位數

設計一個數據結構,包括兩個函數,插入數據和獲得中位數。 利用大根堆和小根堆,其中大根堆維護較小的一半數據,小根堆維護較大的一半數據。 然後根據相應的情況,對兩個堆做相應的堆化操作,以滿足兩個堆中元素數目一致。時間複雜度O(lgn) ex

原创 Fibonacci數列的遞歸優化

// right = F(n-2) , return value = F(n-1) int fibonacci_R(int n, int& right) { if(2 == n) { right = 1; return 1;

原创 已知一個序列seq=[a,b,....,z,aa,ab,...,zz,aaa,aab,....],求任意一個字符串s=[a-z]+在seq中出現的位置

// 26進制,不過有缺點,需要大數處理 #include <iostream> using namespace std; long pow(long x, long y) { long ret = 1; while(y>0)