——Created by SICAU.HarvestWu
1.欧几里得算法:
Euclid(m,n)
while n≠0 do
r=m mod n;
m=n;
n=r;
return m;
2.连续整数检测算法:
Function(m,n)
t=min(m,n);
while t≥0 do
if m mod t == 0 and n mod t == 0
return t;
--t;
3.素数筛选法:
Function(n)
for p=2 to n do
A[p]=p;
for p=2 to sqrt(n) do
if A[p]≠0
j=p*p;
while j<n do
A[j]=0;
j=j+p;
i=0;
for p=2 to n do
if A[p]≠0
L[i]=A[p];
++i;
Return L;
4.矩阵乘积:
Function(A[0..n-1,0..n-1],B[0..n-1,0..n-1])
for i=0 to n-1 do
for j=0 to n-1 do
C[i][j]=0;
for k=0 to n-1 do
C[i,j]=C[i,j]+A[i,k]*B[k,j];
return C;
5.汉诺塔(A->B:A->C,C->B):
void hanoi(int n,int A,int B,int C)
{
if(n>0)
{
hanoi(n-1,A,C,B);
move(A,B);
hanoi(n-1,C,A,B);
}
}
6.选择排序:
(选择当前序列最小值与当前序列首值交换)
SelectionSort(A[0..n-1])
for i=0 to n-1 do
min=i;
for j=i+1 to n-1 do
if A[j]<A[min]
min=j;
swap(A[i],A[min]);
7.冒泡排序:
BubbleSort(A[0..n-1])
for i=0 to n-2 do
for j=i to n-2-i do
if A[j+1]<A[j]
swap(A[j+1],A[j])
8.字符串蛮力匹配:
(逐字符匹配,失败则从下一字符重新开始)
Function(T[0..n-1],P[0..m-1])
for i=0 to n-m do
j=0;
while j<m and P[j]=T[i+j] do
++j;
if j==m return i;
return 0;
9.蛮力平面距离最近两点:
Function(p)
d=∞
for i=0 to n-1 do
for j=i+1 to n do
d=min(d,sqrt((x
i
-x
j
)
2
+(y
i
-y
j
)
2
));
return d;
10.深度优先搜索遍历:
DFS(G)
count=0;
for each vertex v in V do
if v is marked with 0
dfs(v)
dfs(v)//递归访问和 v 相连接未访问顶点
++count;mark v with count;
for each vertex w in V adjacent to v do