re: 與臨時(shí)對象的斗爭(下) Wang Feng 2009-12-07 11:36
ans = a+b+c+d+e+f+....;
如果可以并行的話,這樣很有優(yōu)勢
ans = ((a+b)+(c+d))+((e+f)+(g+h));
你這個(gè)+=雖然省去了一些臨時(shí)對象,不過也只好一個(gè)一個(gè)乖乖地+=了,單個(gè)cpu的時(shí)候沒有事情,多個(gè)cpu的時(shí)候,大好時(shí)間都浪費(fèi)了。
re: 我所理解的歸并排序算法(新) Wang Feng 2009-06-09 11:54
void Merge(T a[], int left, int center, int len)
{
T *t = new T[len-left];//存放被合并后的元素
----------------------------------------------------------------
C++支持這樣的語法
T t[len-left];
我猜想效率會(huì)比
T *t = new T[len-left];
更高
re: 自動(dòng)求導(dǎo)模板庫[未登錄] wang feng 2009-05-02 13:50
能否返回一個(gè)函數(shù)指針?
比如:
typedef double (fp*) ( const vector<double>&, const double);
fp f1;
........
fp f2 = d(f1);
class A
{
private:
struct Handle;
Handle * handle;
public:
.........
};
這樣的話,連一開始的兩個(gè)
class StreamFilter;
class EmitModeConfig;
都不需要了:)
@guest
確實(shí)是這樣的,要解決的話也不是很困難,只要partation時(shí)候把數(shù)組random shuffle一下就可以了:)
re: 對一類因偷換概念而引發(fā)的悖論的思考 Wang Feng 2008-12-03 15:19
@zfv
偶高中的時(shí)候數(shù)學(xué)競賽全國得過獎(jiǎng)的,不太可能沒學(xué)好,更不會(huì)丟人。
re: 對一類因偷換概念而引發(fā)的悖論的思考 Wang Feng 2008-11-25 12:19
@doyle
拜托,我定量計(jì)算之前還有一個(gè)定性分析,那個(gè)是相當(dāng)好理解的。
re: 對一類因偷換概念而引發(fā)的悖論的思考 Wang Feng 2008-11-25 10:28
@doyle
我定量分析中,使用的是一個(gè)數(shù)學(xué)模型;
我不認(rèn)為一個(gè)有著億萬身家的人,還會(huì)像乞丐一樣熱衷于一元錢;
就是說“如果"酷斃"和"帥呆"的貪婪程度完全精確的一樣呢?”這種可能在我的考慮中幾乎是不存在的。
@zuhd
3,5,7,4,0,1
第一次調(diào)用partation(arr, 0, 5)是以1為界分的;
結(jié)果數(shù)組被排成
0 1 7 4 3 5
返回1
@zuhd
如果arr已經(jīng)給定不可更改,那么這個(gè)結(jié)果就是唯一的
我暈倒,有沒有優(yōu)雅一點(diǎn)的,至少短一點(diǎn)的實(shí)現(xiàn)?
@天策魂之音
我用隨機(jī)數(shù)測試過42憶次,沒有出錯(cuò)過;
不知道錯(cuò)誤是什么
@zuhd
"程序的運(yùn)行結(jié)果可以有很多種啊"里邊的程序是指 partation還是nth?
@zuhd
不是,這段代碼目的是:以A為界,把數(shù)組分為兩個(gè)部分,前邊部分?jǐn)?shù)值都是不大于A的,后邊的部分的數(shù)值都是不小于A的
@zuhd
你可以這樣理解:
1) 找到一個(gè)arbitrary value A(這個(gè)數(shù)值應(yīng)該是任意的,我圖寫起來利落,直接指定為數(shù)組的最后一個(gè)元素----比較好的做法是,執(zhí)行之前,在數(shù)組中任意尋找一個(gè)元素與數(shù)組最后一個(gè)元素交換)
2)一個(gè)指針指向數(shù)組的第一個(gè)元素,往向后掃描,找到第一個(gè)大于A的元素,標(biāo)記為arr[a];
3)另外一個(gè)指針指向數(shù)組的最后一個(gè)元素,向前掃面,找到第一個(gè)小于A的元素arr[b];
4)交換arr[a] arr[b]
5)若兩個(gè)指針沒有重合,轉(zhuǎn)到2)
6)將A與兩個(gè)指針重合地點(diǎn)的元素交換
這樣以來,以A為界,把數(shù)組分為兩個(gè)部分,前邊部分?jǐn)?shù)值都是不大于A的,后邊的部分的數(shù)值都是不小于A的
re: pku 2253 Frogger Wang Feng 2008-11-06 22:51
acm中如果涉及到圖的算法,可否直接使用boost graph library?
@cdy20
這樣寫的話,用g++編譯后交換失敗;
用icpc編譯后交換成功,非常奇怪
可有人給出更多測試?
我的編譯器:
$ g++ -v
Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --prefix=/usr --enable-shared --enable-languages=c,c++,fortran,objc,obj-c++,treelang --enable-threads=posix --mandir=/usr/share/man --infodir=/usr/share/info --enable-__cxa_atexit --disable-multilib --libdir=/usr/lib --libexecdir=/usr/lib --enable-clocale=gnu --disable-libstdcxx-pch --with-tune=generic
Thread model: posix
gcc version 4.3.2 (GCC)
$ icpc -v
Version 10.1
@空明流轉(zhuǎn)
@fish_autumn
請求指點(diǎn)
copy( arr, arr+size, ostream_iterator<int>( cout, " " ) );
我覺得這樣寫,看起來容易一點(diǎn)
因?yàn)槎禾?hào)運(yùn)算符優(yōu)先級(jí)最低,于是arr+size中的加號(hào)在逗號(hào)之前;
我的習(xí)慣是先運(yùn)行的更緊密一些;
如你比較喜歡的這個(gè)
if (a == b) a = a + b;
我會(huì)寫成
if (a == b) a = a+b;
或者
if (a == b)
{
a = a+b;
}
或者
if (a == b)
++a;
或者
if (a == b)
{
++a;
}
re: stl奇怪的copy問題[已解決] Wang Feng 2008-11-06 16:39
不要當(dāng)真,當(dāng)我是純粹灌水就好了:)
re: stl奇怪的copy問題[已解決] Wang Feng 2008-11-06 15:58
5 #include <iostream>
6 #include <string>
7 #include <vector>
8 #include <sstream>
9 #include <utility>
10 #include <algorithm>
11 #include <list>
12 #include <map>
13 #include <set>
14 #include <queue>
15 #include <stack>
16 #include <bitset>
17 #include <memory>
18 #include <cstdio>
19 #include <cmath>
20 #include <cstdlib>
21 #include <cctype>
22 #include <cstring>
23 #include <climits>
還有比這更可怕的么?19個(gè)頭文件…………
boost中bind這樣來做:
class button
{
public:
boost::function<void> onClick;
};
class player
{
public:
void play();
void stop();
};
button playButton, stopButton;
player thePlayer;
void connect()
{
playButton.onClick = boost::bind(&player::play, &thePlayer);
stopButton.onClick = boost::bind(&player::stop, &thePlayer);
}
re: RamDisk好處! Wang Feng 2008-06-16 18:00
我這邊是/dev/shm
re: (C++)一個(gè)愚蠢的錯(cuò)誤 Wang Feng 2008-06-09 18:05
#include <cstring>
#include <string>
re: 5.12汶川大地震(一) Wang Feng 2008-05-13 22:23
blessing
re: 今天去the9.com面試,一些考題,一些想法 Wang Feng 2008-05-09 14:48
貌似我是第一個(gè)回帖的,當(dāng)時(shí)看錯(cuò)的題目,貽笑大方了,今天過來翻翻,居然這么多回應(yīng)。
看到面試官說:
另外告訴其他各位朋友,那個(gè)計(jì)算7744這個(gè)數(shù)字的題目,是要用一支筆一張紙嚴(yán)禁的推理出來,用來考查人的分析和推理能力的,不然讓人寫代碼實(shí)現(xiàn)的。
于是就推理一番如下:
-------------------------------------------------------------------------
題目:
1000~10000里面的4位平方數(shù)你給我找出來,數(shù)字的規(guī)則是 abcd, a=b c=d
推理:
1. 依照題意,這個(gè)數(shù)字可以寫為
x = 1100a + 11b = s * s ( 0 < a, b < 10 )
由于1000<= x = s * s <= 10000,故
31 < s <= 100
2. 由于 100a + b = s * s / 11
故s必定能夠被11整除,記
s = 11c ( 2 < c < 10 )
于是得到
11*c * c = 100a + b = y
很顯然y必定為11的倍數(shù)
3. 由于一個(gè)數(shù),如果能夠被11整除,那么它的奇位數(shù)之和與偶位數(shù)之和之差必定能為11所整除(貌似是初中學(xué)過的一個(gè)定理,很容易推導(dǎo),我猜想這個(gè)才是整個(gè)推理過程的要點(diǎn))
很顯然y的“奇位數(shù)之和與偶位數(shù)之和之差”
sub = a + b 必須能夠被11整除
由于限制條件 ( 0 < a, b < 10 )的存在
a+b =11 必定成立
4. 將它代入到
11*c * c = 100a + b
得到
9a = c*c - 1
于是 a = (c-1)(c+1) / (3*3)
5. 很顯然要讓c-1跟c+1同時(shí)都可被3整除是不可能的,所以c+1 c-1這兩個(gè)數(shù)字中,必定有一個(gè)能夠被9整除,另一個(gè)是質(zhì)數(shù)
由于 2 < c < 10
故c = 8
因此s = 88
6. x = 88 * 88
re: 今天去the9.com面試,一些考題,一些想法 Wang Feng 2008-05-06 23:22
@zellux
是我看錯(cuò)了題意,不好意思
re: 今天去the9.com面試,一些考題,一些想法 Wang Feng 2008-05-06 20:26
1000~10000里面的4位平方數(shù)你給我找出來,數(shù)字的規(guī)則是 abcd, a=b c=d
--------------------------------------------------------------------------
可能你記錯(cuò)了范圍了,這個(gè)范圍里邊不存在
int
strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2)
{
if (*s1 == 0)
return 0;
++s1;
++s2;
}
return *(unsigned const char *)s1 - *(unsigned const char *)(s2);
}
re: TopPlayer播放器源碼發(fā)布 Wang Feng 2008-05-04 14:45
對了,編譯器
$ gcc -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../configure --prefix=/usr --enable-shared --enable-languages=c,c++,fortran,objc,obj-c++,treelang --enable-threads=posix --mandir=/usr/share/man --enable-__cxa_atexit --disable-multilib --libdir=/usr/lib --libexecdir=/usr/lib --enable-clocale=gnu --disable-libstdcxx-pch --with-tune=generic
Thread model: posix
gcc version 4.3.0 (GCC)
re: TopPlayer播放器源碼發(fā)布 Wang Feng 2008-05-04 14:44
第一個(gè)文件就編譯不過去
部分輸出:
CLV_ListView.c:2831: error: ‘CIs_ListViewData’ has no member named ‘m_pColumns’
CLV_ListView.c:2834: error: ‘rInvalid’ undeclared (first use in this function)
CLV_ListView.c:2835: error: ‘CIs_ListViewData’ has no member named ‘m_rClient’
CLV_ListView.c:2837: error: ‘CIs_ListViewData’ has no member named ‘m_pColumns’
CLV_ListView.c:2838: error: ‘CIs_ListViewData’ has no member named ‘m_hWnd’
CLV_ListView.c:2838: error: ‘FALSE’ undeclared (first use in this function)
CLV_ListView.c: In function ‘CLV_Invalidate’:
CLV_ListView.c:2849: error: ‘CIs_ListViewData’ has no member named ‘m_hWnd’
CLV_ListView.c:2849: error: ‘FALSE’ undeclared (first use in this function)
CLV_ListView.c: In function ‘RemoveSelectedItem’:
CLV_ListView.c:2859: error: ‘BOOL’ undeclared (first use in this function)
CLV_ListView.c:2859: error: expected ‘;’ before ‘bMoved’
CLV_ListView.c:2864: error: ‘struct <anonymous>’ has no member named ‘m_hPlaylistViewControl’
CLV_ListView.c:2865: error: ‘struct <anonymous>’ has no member named ‘m_hPlaylistViewControl’
CLV_ListView.c:2868: error: ‘BROWSEINFO’ undeclared (first use in this function)
CLV_ListView.c:2868: error: expected ‘;’ before ‘browseinfo’
CLV_ListView.c:2869: error: ‘LPITEMIDLIST’ undeclared (first use in this function)
CLV_ListView.c:2869: error: expected ‘;’ before ‘itemlist’
CLV_ListView.c:2873: error: ‘browseinfo’ undeclared (first use in this function)
CLV_ListView.c:2873: error: ‘HWND’ undeclared (first use in this function)
CLV_ListView.c:2873: error: expected ‘;’ before ‘G_MTabCtrl’
CLV_ListView.c:2877: error: ‘BIF_EDITBOX’ undeclared (first use in this function)
CLV_ListView.c:2882: error: ‘itemlist’ undeclared (first use in this function)
CLV_ListView.c:2882: warning: implicit declaration of function ‘SHBrowseForFolder’
CLV_ListView.c:2885: warning: implicit declaration of function ‘SHGetPathFromIDList’
CLV_ListView.c:2885: error: ‘struct <anonymous>’ has no member named ‘main_text_last_browsed_dir’
CLV_ListView.c:2871: warning: unused variable ‘directorychoice’
CLV_ListView.c:2890: error: ‘struct <anonymous>’ has no member named ‘m_hPlaylistViewControl’
CLV_ListView.c:2893: warning: implicit declaration of function ‘lstrcpyn’
CLV_ListView.c:2901: warning: implicit declaration of function ‘lstrcpy’
CLV_ListView.c:2901: error: ‘struct <anonymous>’ has no member named ‘main_text_last_browsed_dir’
CLV_ListView.c:2902: warning: implicit declaration of function ‘lstrcat’
CLV_ListView.c:2903: error: ‘bMoved’ undeclared (first use in this function)
CLV_ListView.c:2903: warning: implicit declaration of function ‘MoveFile’
CLV_ListView.c:2906: warning: implicit declaration of function ‘CPLI_SetPath’
CLV_ListView.c:2912: error: ‘struct <anonymous>’ has no member named ‘m_hPlaylistViewControl’
CLV_ListView.c:2914: error: ‘struct <anonymous>’ has no member named ‘m_hPlaylistViewControl’
CLV_ListView.c:2858: warning: unused variable ‘cNewPath’
CLV_ListView.c:2857: warning: unused variable ‘cPath’
CLV_ListView.c: At top level:
CLV_ListView.c:2916: error: expected ‘=’, ‘,’, ‘;’, ‘a(chǎn)sm’ or ‘__attribute__’ before ‘CPlaylistWindow_CB_onAppMessage’
CLV_ListView.c:2958: error: expected ‘)’ before ‘hWnd’
CLV_ListView.c:3018: error: expected ‘)’ before ‘hWnd’
make: *** [CLV_ListView.o] Error 1
re: 模板參數(shù)名命名慣例 Wang Feng 2008-04-21 22:12
說起宏定義,其實(shí)在文件前面定義,用完了之后在文件最后取消是個(gè)很好的習(xí)慣
比如
#define PI 3.1415926535897932384626433
.....
#undef PI
re: 編程之美:讓CPU占用率曲線聽你指揮 Wang Feng 2008-04-18 17:15
unix上怎么辦?