摘要: 了解C++的童鞋都知道algorithm里面有个next_permutation可以求下一个排列数,通过《STL 源码剖析》(或者自己读代码)可以知道其实现,比如:
abcd next_permutation -> abdc
那么,为什么abcd的下一个是abdc而不是acbd呢?
阅读全文
posted @
2010-02-24 00:11 尹东斐 阅读(3404) |
评论 (7) |
编辑 收藏
条件:1K内存,1MHzCPU,每秒可以改变2^20次状态。问:一个程序最长的运行时间是多少?
答: 首先程序是确定性的,就说明内存的状态不会重复,否则就永远结束不了。从这一点出发,可以知道内存的状态共有 2^8k , 然后CPU每秒改变 2^20 个状态,所以这台计算机最长出现不重复的状态 2^(8k-20)秒。 |
posted @
2009-10-15 10:57 尹东斐 阅读(3597) |
评论 (12) |
编辑 收藏
题目描述:设有n个正整数,将它们联接成一排,组成一个最小的多位整数。
程序输入:n个数程序输出:联接成的多位数
例如:n=2时,2个整数32,321连接成的最小整数为:32132,n=4时,4个整数55,31,312, 33 联接成的最小整数为:312313355
[题目要求]1. 给出伪代码即可,请给出对应的文字说明,并使用上面给出的例子试验你的算法。2. 给出算法的时间空间复杂度。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));
}
证明:
假设: 排序后的 a0a1...an不是最小的,那么存在a0a1...ajai....an<a0a1...an,且ajai > aiaj.
那么交换ajai会使可以使a0a1...an更小,与假设a0a1...ajai....an<a0a1...an矛盾。
证明完毕。
posted @
2009-06-04 23:49 尹东斐 阅读(695) |
评论 (2) |
编辑 收藏
摘要: 这一块主要是讲move语义的,我认为这是在C++0x中,最好的特性之一,因为它几乎可以完全透明的提高效率。在Stephan T. Lavavej这篇帖子之后,有很多评论,大体上认为C++因为这些特性而变得更复杂了,而难以掌握,另初学者望而生畏。但是我认为这是值得的,因为C++的宗旨是:“don't pay for what you don't use 不要为你不使用的东西而付出代价...
阅读全文
posted @
2009-05-28 20:51 尹东斐 阅读(1583) |
评论 (0) |
编辑 收藏
摘要:
最近最大的新闻莫过于微软发布Visual Studio2010了,对c++的支持更进一步,其intellsence的解析也使用了和以前完全不同的方法(以前是靠编译器,现在是独立inellsence单元),番茄可能要被打入冷宫了。Stephan T. Lavavej在Visual c++ Team Blog上发布了VC10对C++0x标准的支持情况,包括:lambdas, auto,...
阅读全文
posted @
2009-05-27 23:17 尹东斐 阅读(1796) |
评论 (3) |
编辑 收藏
最近项目里总是要对很庞大的公式求导,很烦人,手工求导容易出错。
当然MATLAB是个好选择,不过当它要钱的时候,您可能就不这么认为了。
于是,实现了一个可以
编译期求导(不用担心运行时负担)的小型库,还不完全,仅支持多项式,sin,cos,pow,exp,log等函数求导。
后期的表达式优化做的不是很好。
下面是一些测试代码,完整的源码在
http://www.boostpro.com/vault/index.php?action=downloadfile&filename=[math]AD.zip实现部分很复杂,请多多指教。
只有1个函数, d(...)
支持高阶,多元求导。
d(exp, var)(value1, value2, ...)
exp内可以有多个变量,var表示要对其求导的变量,value表示求导以后用于计算表达式的变量的值。
比如:
d(d(x*x*x, x),x)(3.0) 表示对x*x*x求二阶导数在x=3.0时候的值。
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把求导后的表达式输出,不用给变量给值。
cout<<d(x*x, x) // 结果是:2*x
这里没有用任何迭代,是直接对表达式求导的。返回值是求导后的表达式,本质是一个仿函数。可以用boost::function保存起来使用。
例如:
boost::function<double (double)> df = d(pow(x, const_<10>::type()), x); //df 参数为1个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
输出结果如下:
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
posted @
2009-05-01 23:50 尹东斐 阅读(2574) |
评论 (6) |
编辑 收藏