摘要: 了解C++的童鞋都知道algorithm里面有個(gè)next_permutation可以求下一個(gè)排列數(shù),通過(guò)《STL 源碼剖析》(或者自己讀代碼)可以知道其實(shí)現(xiàn),比如:
abcd next_permutation -> abdc
那么,為什么abcd的下一個(gè)是abdc而不是acbd呢?
閱讀全文
摘要: 怎么取得析構(gòu)函數(shù)的地址???
閱讀全文
摘要: C++下的垃圾回收機(jī)制可能會(huì)在下個(gè)版本加入,我只是想通過(guò)實(shí)例,分析垃圾回收器的內(nèi)部機(jī)制,深入了解以后,在以后
的項(xiàng)目中,就可以對(duì)是否需要垃圾回收功能做出準(zhǔn)確的判斷。
閱讀全文
條件:1K內(nèi)存,1MHzCPU,每秒可以改變2^20次狀態(tài)。問(wèn):一個(gè)程序最長(zhǎng)的運(yùn)行時(shí)間是多少?
答: 首先程序是確定性的,就說(shuō)明內(nèi)存的狀態(tài)不會(huì)重復(fù),否則就永遠(yuǎn)結(jié)束不了。從這一點(diǎn)出發(fā),可以知道內(nèi)存的狀態(tài)共有 2^8k , 然后CPU每秒改變 2^20 個(gè)狀態(tài),所以這臺(tái)計(jì)算機(jī)最長(zhǎng)出現(xiàn)不重復(fù)的狀態(tài) 2^(8k-20)秒。 |
題目描述:設(shè)有n個(gè)正整數(shù),將它們聯(lián)接成一排,組成一個(gè)最小的多位整數(shù)。
程序輸入:n個(gè)數(shù)程序輸出:聯(lián)接成的多位數(shù)
例如:n=2時(shí),2個(gè)整數(shù)32,321連接成的最小整數(shù)為:32132,n=4時(shí),4個(gè)整數(shù)55,31,312, 33 聯(lián)接成的最小整數(shù)為:312313355
[題目要求]1. 給出偽代碼即可,請(qǐng)給出對(duì)應(yīng)的文字說(shuō)明,并使用上面給出的例子試驗(yàn)?zāi)愕乃惴ā?. 給出算法的時(shí)間空間復(fù)雜度。3. 證明你的算法。(非常重要)

#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <iostream>
#include <iterator>
#include <sstream>
using namespace std;

struct Less


{
bool operator()(long i, long j)

{
static stringstream ss;
ss.clear();
ss<<i<<" "<<j;
string stri,strj;
ss>>stri>>strj;
return (i*powl(10,strj.length())+j) < (j*powl(10,stri.length()) +i);
}
};

int main()


{

long x[] =
{565, 56, 5655};
sort(x, x+3, Less());
copy(x, x+3, ostream_iterator<long>(cout));
}
證明:
假設(shè): 排序后的 a0a1...an不是最小的,那么存在a0a1...ajai....an<a0a1...an,且ajai > aiaj.
那么交換ajai會(huì)使可以使a0a1...an更小,與假設(shè)a0a1...ajai....an<a0a1...an矛盾。
證明完畢。
摘要: 這一塊主要是講move語(yǔ)義的,我認(rèn)為這是在C++0x中,最好的特性之一,因?yàn)樗鼛缀蹩梢酝耆该鞯奶岣咝省T赟tephan T. Lavavej這篇帖子之后,有很多評(píng)論,大體上認(rèn)為C++因?yàn)檫@些特性而變得更復(fù)雜了,而難以掌握,另初學(xué)者望而生畏。但是我認(rèn)為這是值得的,因?yàn)镃++的宗旨是:“don't pay for what you don't use 不要為你不使用的東西而付出代價(jià)...
閱讀全文
摘要:
最近最大的新聞莫過(guò)于微軟發(fā)布Visual Studio2010了,對(duì)c++的支持更進(jìn)一步,其intellsence的解析也使用了和以前完全不同的方法(以前是靠編譯器,現(xiàn)在是獨(dú)立inellsence單元),番茄可能要被打入冷宮了。Stephan T. Lavavej在Visual c++ Team Blog上發(fā)布了VC10對(duì)C++0x標(biāo)準(zhǔn)的支持情況,包括:lambdas, auto,...
閱讀全文
摘要: 類(lèi)實(shí)例能做做模板參數(shù)嗎?
MyClass
t; 閱讀全文
最近項(xiàng)目里總是要對(duì)很龐大的公式求導(dǎo),很煩人,手工求導(dǎo)容易出錯(cuò)。
當(dāng)然MATLAB是個(gè)好選擇,不過(guò)當(dāng)它要錢(qián)的時(shí)候,您可能就不這么認(rèn)為了。
于是,實(shí)現(xiàn)了一個(gè)可以
編譯期求導(dǎo)(不用擔(dān)心運(yùn)行時(shí)負(fù)擔(dān))的小型庫(kù),還不完全,僅支持多項(xiàng)式,sin,cos,pow,exp,log等函數(shù)求導(dǎo)。
后期的表達(dá)式優(yōu)化做的不是很好。
下面是一些測(cè)試代碼,完整的源碼在
http://www.boostpro.com/vault/index.php?action=downloadfile&filename=[math]AD.zip實(shí)現(xiàn)部分很復(fù)雜,請(qǐng)多多指教。
只有1個(gè)函數(shù), d(...)
支持高階,多元求導(dǎo)。
d(exp, var)(value1, value2, ...)
exp內(nèi)可以有多個(gè)變量,var表示要對(duì)其求導(dǎo)的變量,value表示求導(dǎo)以后用于計(jì)算表達(dá)式的變量的值。
比如:
d(d(x*x*x, x),x)(3.0) 表示對(duì)x*x*x求二階導(dǎo)數(shù)在x=3.0時(shí)候的值。
d(d(x*x*y, x), y)(3.0, 4.0) 表示d(x*x*y)/(dxdy)在x=3.0,y=4.0的值。
d(d(x*x*x, x) +d(y*x, y), y) (2.0) 則表示 (d(x*x*x)/dx + d(y*x)/dy)/dy == 0。
可以直接用cout把求導(dǎo)后的表達(dá)式輸出,不用給變量給值。
cout<<d(x*x, x) // 結(jié)果是:2*x
這里沒(méi)有用任何迭代,是直接對(duì)表達(dá)式求導(dǎo)的。返回值是求導(dǎo)后的表達(dá)式,本質(zhì)是一個(gè)仿函數(shù)。可以用boost::function保存起來(lái)使用。
例如:
boost::function<double (double)> df = d(pow(x, const_<10>::type()), x); //df 參數(shù)為1個(gè)double,返回double
然后就可以在任何地方使用 df 了:
double res = df(3.0) // res == pow(3, 9)
1
#include "ad.h"
2
#include <iostream>
3
#include <iterator>
4
5
using namespace std;
6
7
int main()
8

{
9
variable<0>::type x;
10
variable<1>::type y;
11
12
double res[14];
13
14
res[0] = d(pow(x, const_<10>::type()), x)(2.0);
15
16
res[1] = d(x * x * x, x)(2.0);
17
res[2] = d(x + x + x, x)(2.0);
18
res[3] = d(x - x - x, x)(2.0);
19
res[4] = d(x / x, x)(2.0);
20
21
res[5] = d(pow(x, var(3.0)), x)(2.0);
22
res[6] = d(pow(var(3.0), x), x)(2.0);
23
res[7] = d(pow(x, x), x)(2.0);
24
25
res[8] = d(log(x), x)(2.0);
26
res[9] = d(exp(x), x)(2.0);
27
28
res[10] = d(sin(x), x)(2.0);
29
res[11] = d(cos(x), x)(2.0);
30
31
res[12] = d(d(sin(x) * cos(y), x), y)(2.0, 3.0);
32
33
res[13] = (d(log(x) + x, x) * x)(2.0);
34
35
copy(res, res + 14, ostream_iterator<double>(cout, "\n"));
36
37
cout<<d(pow(x, const_<10>::type()), x)<<endl;
38
39
cout<<d(x * x * x, x)<<endl;
40
cout<<d(x + x + x, x)<<endl;
41
cout<<d(x - x - x, x)<<endl;
42
cout<<d(x / x / x, x)<<endl;
43
44
cout<<d(pow(x, var(3.0)), x)<<endl;
45
cout<<d(pow(var(3.0), x), x)<<endl;
46
cout<<d(pow(x, x), x)<<endl;
47
48
cout<<d(log(x), x)<<endl;
49
cout<<d(exp(x), x)<<endl;
50
51
cout<<d(sin(x), x)<<endl;
52
cout<<d(cos(x), x)<<endl;
53
54
cout<<d(d(sin(x) * cos(y), x), y)<<endl;
55
56
cout<<(d(log(x) + x, x) * x)<<endl;
57
58
return 0;
59
}
60
輸出結(jié)果如下:
1
512
2
12
3
3
4
-1
5
0
6
12
7
9.88751
8
6.77259
9
0.5
10
7.38906
11
-0.416147
12
0.909297
13
-0.0587266
14
3
15
pow(x,9)
16
(((x+x)*x)+(x*x))
17
3
18
-1
19
(-1/(x*x))
20
(pow(x,3)*(3*(1/x)))
21
(pow(3,x)*log(3))
22
(pow(x,x)*(log(x)+1))
23
(1/x)
24
exp(x)
25
cos(x)
26
sin(x)
27
(cos(x)*sin(y))
28
(((1/x)+1)*x)
29