18 May 2011

CMD命令中空格的解决办法


有的时候产品容易被默认安装到c:\Program Files\这样包含空格的路径下,那么在命令行中输入一个带空格的file path作为参数往往会出错。所以大家都流传着这样一种说法:产品安装的时候选择的目录千万不要包含空格。

但是,一个那么大的产品装好了,使用命令行配置的时候发现path带空格命令过不去,而卸载重装太恐怖了,怎么办?

现在有两个办法来解决这个问题



1)用缩写。比如c:\Program Files缩写为c:\Progra~1
再来刨根问底查查这个命名是否有规则,于是找到:
文件夹(sub-directry)名称,以前是不允许带空白的,后来允许带空白,但由于有了空白,许多命令出现二义性,于是采用双引号括起来的办法。例如:
cd Documents and Settings
按老定义 等于 CD Documents, CD 命令找不到名叫Documents 的 directry
于是采用双引号:
cd “Documents and Settings“
但用到 set PATH 时很麻烦,名字太长,双引号时常括错。于是采用8个字符缩写,即写头六个字母(略去空白),另加波浪号和1。例如:
"Documents and Settings“ -- DOCUME~1
"Local Settings" -- LOCALS~1 (注意略去空白,用了第二个词的字母,凑成六个,再加波浪号和1)。
于是,这种方法成了规定。
再来个十万个为什么的下一个,如果多个文件前6字符一样怎么办?为什么最后是1而不是0或者其他数字呢?看看这个例子
假设下面是你的C盘根目录中的文件夹:
Program Files
Progra file
Progra zhang
则三个目录分别表示为:C:\Progra~1; C:\Progra~2; C:\Progra~3;
2)绕过去,创建一个镜像。例如在cmd中输入 subst w: "C:\Documents and Settings\hopeshared"。然后就可以直接用w:\替代C:\Documents and Settings\hopeshared了

17 May 2011

Euler Project5 欧拉工程5



2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

找到可以整除1-20的最小正整数. 分析过程:

% 整除1-20,其实就是1-20的最小公倍数,计算时可以精简:
% 1-20只需要求[2,3,4,5,6,7,11,13,17,19]的最小公倍数即可
% 其余的
% 1不用计算
% 8 = 2*4
% 9 = 3*3(第二个3来自6=2*3)
% 10 = 2*5
% 12 = 3*4
% 14 = 2*7
% 16 = 2*8
% 18 = 2*9 = 2*3*3(第二个3来自6=2*3)
% 20 = 2*10 = 2*5*2(第二个2来自4=2*2)

==============
代码:
function result = euler5()
tic;
result = prod([2,3,4,5,6,7,11,13,17,19]); % => 8,9(3*3),10,12,14,15,16(2*4*2),18,20
toc;
end

运行时间及结果:
% Elapsed time is 0.000012 seconds.
% ans =
% 232792560

16 May 2011

Euler Project4 欧拉工程4



如果一个数字正写和反写结果一样,就称为回文数,请找到可以表示为2个三位数乘积的最大回文数.
%% 3 digital number which remains same from both direction, like 9009
% == from small to big
% Elapsed time is 43.379476 seconds.
% ans =
% 906609
% == from big to small
% Elapsed time is 5.504064 seconds.
% ans =
% 906609
function result = euler4()
tic;
result = 0;
for i=999*999:-1:100*100 % ends with 0, jump over
if mod(i,10)==0
continue;
end
n = num2str(i);
if strcmp(n,n(end:-1:1))
flag = 0;
for j=999:-1:900 % max, so from 999 to 900, try
if i/j<=999 && i==fix(i/j)*j % i/j must be smaller than 999, and i/j must be integer flag = j; break; end end if flag~=0 result = i; break; end end end [j,result/j] toc; end

结果
% Elapsed time is 5.504064 seconds.
% ans =
% 906609

14 May 2011

Euler Project3 欧拉工程3



求600851475143的最大质因数
%% 600851475143's biggest prime factor
% Elapsed time is 55.277722 seconds.
% ans =
% 6857
function result = euler3()
tic;
result = 0;
n = 600851475143;
for i=fix(sqrt(n)):-1:3
if isprime(i)==1 && mod(n,i)==0
result = i;
break;
end
end
toc;
end

13 May 2011

Euler Project2 欧拉工程2



Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

求斐波那契数列中小于4,000,000的值中的偶数项之和.
%% sum of even value elements from fibonacci list smaller than 4,000,000
% Elapsed time is 0.000011 seconds.
% ans =
% 4613732
function result = euler2()
tic;
a = 1;
b = 2;
result = 2;
while (a<4000000 && b<4000000) a = a+b; if mod(a,2)==0 result = result+a; end b = a+b; if mod(b,2)==0 result = result+b; end end toc; end

10 May 2011

Euler Project1 欧拉工程1



欧拉工程是一个用编程来解决一连串数学问题的项目。发现它对训练数学及编程挺有作用的。网上也有许多人做上面的题目并发布自己的算法。

上面共有337题,分几个等级,一级比一级难。完成它绝对是时间及精力及耐力及智力的较量。

解决问题的代码贴在这里.主要用matlab,偶尔python,很少c/c++.有时会matlab调python解决,只为快速解决问题. Question 1:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.
解决
%% sum of number below 1000 and can be divided by 3 or 5
% Elapsed time is 0.002665 seconds.
% ans =
% 233168
function result = euler1()
tic
result = sum(unique([3:3:1000,5:5:999]))
toc;
end