编程:找出数组中的特殊数

Categories: Program
Tags: No Tags
Comments: 1 Comment
Published on: 2012/11/28

若干年前在电子bbs【自由空间】上的一个题目,来源链接:http://thuee.net/bbscon.php?bid=13&id=64451

假设有一个数组P有3N+1个整数,其中有N个数每个出现3次,还有一个特殊数仅出现过一次,比如{a,a,a,b,b,b,c}。要求只遍历一遍数组将这个数找出来,空间复杂度O(1)。

如果是每个数只出现两次,那么只需要把所有的数异或起来就可以得到最终的结果了。那出现三次呢?下面这个程序巧妙地实现了这一点:
(more...)

看到一段很喜欢的程序

Categories: Program
Tags: No Tags
Comments: 2 Comments
Published on: 2012/10/05
#include <stdio.h>
void main() {
    double sided;
    unsigned letter;
    short happiness;
    long memories;
    printf("I love you.\n");
}

来源:http://www.guokr.com/group/posts/60/

清华百年校庆献礼——Matlab版老校歌

Categories: Program
Tags: No Tags
Comments: 2 Comments
Published on: 2011/04/21

当年上matlab课,要写个生成音乐的程序,我就写了这个老校歌。当时大作业里面的“电灯比油灯进步多了”那一段用于语音处理的音频想必很多电子系的兄弟姐妹记忆犹新吧。
当时交上去的版本其实还有很多我不满意的地方,某一年翻出来又给改了一下,用了shenhuanyan mm的某个程序里面的音色,修改了音符的轮廓,加了个颤。另外和声是依照的是何一林夫人版合唱谱。

没有matlab的同学可以直接听这个wav文件:Matlab老校歌

新的版本我还是比较满意的,程序如下 (保存为playlxg.m即可):

?View Code MATLAB
%% play "老校歌"
%% written by vichare@vichare.net
function y = playlxg()
global lxg;
if length(lxg) < 5;
    rhythm1 = [1 1 3 5 5 6 1 6 5 5 3 3 5 3 1 6 1 3 5 6 6 6 1 5 3 2 3 2 1 2 5 5 14 5 6 6 7 6 5 1 1 6 1 5 6 5 6 6 5 3 2 2 3 5 1 1 1 3 2 3 2 1 6 6 5 3 2 2 3 1 1 1 6 6 5 5 6 5 2 3 5 1 1 6 6 5 5 6 5 2 3 2 1;
    0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 1 0 0 0 0 0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5 1 1 1 1 2 1.5 0.5 1 1 1 1 2 1 3 1 3 1 1 1 1 1 1 2 1 3 1 3 1 1 1 1 1 1 2];
    y1 = generaterhythm(rhythm1, 3); % in bE tune
    y2 = generaterhythm(rhythm2, 3); % in bE tune
    y3 = generaterhythm(rhythm3, 3); % in bE tune
    y4 = generaterhythm(rhythm4, 3); % in bE tune
    %plot(y);
    lxg = (y1+y2+y3/2+y4/2)/3;
    sound(lxg); % play it!
    %wavwrite(lxg, 8000, 'lxg.wav'); % write to file
else
    sound(lxg); % play it!
end
 
%% generate a piece of rhythm
function y = generaterhythm(rhythm, basetune)
% GENERATERHYTHM function.
% rhythm is a 3 * len matrix
% for each column, the element on the first row
% shows the 'sound name' of one tune, the one
% on the second row shows the sound step of that tune,
% and the one on the third row shows how long
%in seconds it should lasts.
fs = 8000;
soundpos = [0 2 4 5 7 9 11 0:12];
y = zeros(1, sum(rhythm(3,:))*fs + 1); % initialize
curpos = 1;
for count = 1 : length(rhythm)
cursoundname = rhythm(1, count);
cursoundpos = soundpos(cursoundname);
curfreq = 220 * 2 .^ ((cursoundpos + basetune + 3) / 12 + rhythm(2, count));
cursound = generatetune(curfreq, rhythm(3, count), fs);
y(1,curpos:(curpos + length(cursound) - 1)) = cursound;
curpos = curpos + length(cursound);
end
 
%% generate a piece of sound with the frequence of freq
function y = generatetune (freq, time, fs)
y1 = generatetune2 (freq + 3, time, fs);
y2 = generatetune2 (freq - 3, time, fs);
y3 = generatetune2 (freq, time, fs);
y = (y1 + y2) / 8 + y3 * (3 / 4);
 
%% generate a piece of sound with the frequence of freq
function y = generatetune2 (freq, time, fs)
c = [ 1 0.2 0.4 0.08 0.2 0.05 0.1 0.04 ];
t = 0:1/fs:(time - 1/fs);
y = zeros(1, length(t));
for k = 1 : 8
    y = y + c(k) * sin(t*k*freq*2*pi);
end
%%y = 0.6 * sin(t*freq*2*pi) + 0.1 * sin(t*freq*4*pi) + 0.2 * sin(t*freq*6*pi);
for count1 = 1 : length(y)
    y(1, count1) = y(1, count1) * amendment(count1 / 2000, length(y) / 2000);
end
 
%% amendment of the shape of the wave
function y = amendment(p, l) % 0 <= p <= 1
if p < 0.2
y = p * 5;
elseif p < 0.3
y = 1.8 - p * 4;
else
    y = 0.6 * exp((0.3 - p)/5);
%%elseif p < (l - 0.2)
%%y = 0.6;
%%else
%%y = (l - p) * 3;
end

从Google weather api读取天气预报的python脚本

Categories: Program
Tags: No Tags
Comments: No Comments
Published on: 2009/09/15
?View Code PYTHON
import urllib
from xml.dom import minidom
 
proc = lambda tnlist, conds : [ 
    [temp.getAttribute('data') for temp in conds.getElementsByTagName(tagname)][0] 
    for tagname in (tnlist) ]
 
data = urllib.urlopen('http://www.google.com/ig/api?weather=Beijing&hl=zh-cn').read().decode('gbk').encode('utf8')
xmldoc = minidom.parseString(data)
curconds = xmldoc.getElementsByTagName("current_conditions")
(t, h, c, w) = proc(('temp_c', 'humidity', 'condition', 'wind_condition'), curconds[0])
print t
print h
print c
print w
 
for cond in xmldoc.getElementsByTagName("forecast_conditions"):
    (d, l, h, c) = proc(('day_of_week', 'low', 'high', 'condition'), cond)
    print '%s: %s/%s %s' % (d, l, h, c)

利用union做类型转换

Categories: Program
Tags: No Tags
Comments: No Comments
Published on: 2009/09/10
#include 
 
using namespace std;
 
union conv {double arg; struct fl{float real; float imag;}f;};
 
int main()
{
    float farr[] = {1.0, 2.0};
    double* pd = (double*)(farr);
    conv c;
    c.arg = *pd;
    cout << c.f.real << endl;
    cout << c.f.imag << endl;
    return 0;
}

高效能编程的七个好习惯(zz)

Categories: Program
Tags: No Tags
Comments: No Comments
Published on: 2008/10/30

from[http://blog.youxu.info/2008/10/29/seven-habits-of-highly-effective-programmers/]

这七条都是我这个不怎么高效能编程的人悟到的. 不权威, 不一定全对.

(more...)

page 1 of 1
Welcome , today is Wednesday, 2018/02/21