wills经典算法Word下载.docx

上传人:b****6 文档编号:16216306 上传时间:2022-11-21 格式:DOCX 页数:45 大小:733.68KB
下载 相关 举报
wills经典算法Word下载.docx_第1页
第1页 / 共45页
wills经典算法Word下载.docx_第2页
第2页 / 共45页
wills经典算法Word下载.docx_第3页
第3页 / 共45页
wills经典算法Word下载.docx_第4页
第4页 / 共45页
wills经典算法Word下载.docx_第5页
第5页 / 共45页
点击查看更多>>
下载资源
资源描述

wills经典算法Word下载.docx

《wills经典算法Word下载.docx》由会员分享,可在线阅读,更多相关《wills经典算法Word下载.docx(45页珍藏版)》请在冰豆网上搜索。

wills经典算法Word下载.docx

whilej<

50000do

begin

p[j]:

inc(j,i);

end;

inc(i);

end;

fori:

=1to10doifp[i]thenwriteln(i);

//getprime

functionsuperprime(z:

ifp[z]thenexit(true)elseexit(false);

//superprime

//main

begin

readln(n,m);

writeln(gcd(n,m));

writeln(lcm(n,m));

writeln(prime(n));

getprime;

writeln(superprime(n));

end.

//最小生成树--huffman

programhuffmantree;

constmaxn=100;

m=2*100-1;

type

node=record

data:

prt,lch,rch:

ty=array[1..m]ofnode;

i,j,k,n:

tree:

ty;

procedurehuffman(vartree:

ty);

functionmin(h:

var

m1,p,i:

m1:

=maxlongint;

forp:

=1tohdo

if(tree[p].prt=0)and(m1>

tree[p].data)thenbegin

i:

=p;

=tree[p].data;

min:

=i;

//min

fork:

=n+1to2*n-1do

=min(k-1);

tree[i].prt:

=k;

tree[k].lch:

j:

tree[j].prt:

tree[k].rch:

=j;

tree[k].data:

=tree[i].data+tree[j].data;

//huffman

fillchar(tree,sizeof(tree),0);

readln(n);

fori:

=1tondoread(tree[i].data);

huffman(tree);

=1to2*n-1dowriteln(tree[i].data);

//最小生成树-----prim算法

programprimtree;

constmaxn=1000;

map:

array[1..maxn,1..maxn]oflongint;

cost:

array[1..maxn]oflongint;

visit:

array[1..maxn]ofboolean;

i,j,n,m,x,y,v:

functionprim:

vari,j,min,mini,ans:

ans:

=0;

=1tondobegin

visit[i]:

cost[i]:

=2tondo

ifmap[1,i]<

>

0thencost[i]:

=map[1,i];

visit[1]:

=true;

=1ton-1do

begin

min:

forj:

=1tondo

ifnot(visit[j])and(cost[j]<

min)thenbegin

min:

=cost[j];

mini:

end;

visit[mini]:

inc(ans,min);

ifnotvisit[j]and(map[mini,j]>

0)and(map[mini,j]<

cost[j])

thencost[j]:

=map[mini,j];

end;

exit(ans);

//prim

fillchar(map,sizeof(map),0);

=1tomdo

begin

readln(x,y,v);

if(map[x,y]=0)or(map[x,y]>

v)then

map[x,y]:

=v;

map[y,x]:

writeln(prim);

//最小生成树--------kruskal算法

programkruskal;

const

maxn=1000;

type

rqmap=record

s,t,v:

array[1..maxn*maxn]ofrqmap;

father:

n,m,i,ingraph,ans:

procedureqsort(l,r:

longint);

vari,j,x:

t:

rqmap;

=l;

j:

=r;

x:

=map[(i+j)shr1].v;

repeat

whilemap[i].v<

xdoinc(i);

whilemap[j].v>

xdodec(j);

ifi<

=jthen

begin

t:

=map[i];

map[i]:

=map[j];

map[j]:

=t;

inc(i);

dec(j);

untili>

j;

ifl<

jthenqsort(l,j);

ifr>

ithenqsort(i,r);

//qsort

functionfind(x:

iffather[x]=xthenexit(x);

father[x]:

=find(father[x]);

exit(father[x]);

//find

procedureunion(a,b:

father[find(a)]:

=find(father[b]);

//union

=1tondofather[i]:

=1tomdowithmap[i]doreadln(s,t,v);

qsort(1,m);

ans:

ingraph:

=1;

whileingraph<

ndo

inc(i);

iffind(map[i].s)<

find(map[i].t)then

inc(ingraph);

inc(ans,map[i].v);

union(map[i].s,map[i].t);

writeln(ans);

end.}

{最小生成树例题数据:

610

126

131

145

235

253

536

566

634

642

435

ans=15}

//带权图的最短路---dijkstra算法

programdijkstrashort;

constmaxn=5;

a:

n,k,i,j:

mark:

procedurereaddata;

readln(n,k);

forj:

=1tondo

read(cost[i,j]);

//readdate

proceduredijkstra;

vari,j,min,minj,temp:

fillchar(mark,sizeof(mark),false);

=1tondoa[i]:

a[k]:

=1ton-1do

=1tondo

ifnot(mark[j])and(a[j]<

min)then

begin

min:

=a[j];

minj:

end;

mark[minj]:

ifnot(mark[j])and(cost[minj,j]>

0)then

temp:

=a[minj]+cost[minj,j];

iftemp<

a[j]thena[j]:

=temp;

//dijksra

readdata;

dijkstra;

=1ton-1dowrite(a[i],'

'

);

writeln(a[n]);

{单源最短路数据:

51

0431000100040100075310000171000710210005720

ans:

=04346

//所有顶点间的最短路径----floyed算法

programfloyedshort;

constmaxn=200;

n,i,j,x,y:

vari,j:

readln(n,x,y);

read(cost[i,j]);

if(cost[i,j]=0)or(cost[i,j]=-1)thenbegin

cost[i,j]:

=1000000000;

p[i,j]:

a[i,j]:

=cost[i,j];

//readdata

procedurefloyed;

vari,j,k:

fork:

fori:

ifa[i,k]+a[k,j]<

a[i,j]then

begin

a[i,j]:

=a[i,k]+a[k,j];

p[i,j]:

end;

//floyed

procedurepath(i,j:

vark:

k:

=p[i,j];

ifk<

0then

path(i,k);

write(k,'

path(k,j);

//path

fillchar(cost,sizeof(cost),0);

floyed;

writeln(a[x,y]);

ifp[x,y]=0thenwriteln('

noroad'

)elsebegin

write(x,'

path(x,y);

write(y,'

{floyed数据:

514

=

4

134}

//dp动态规划

//最大不下降子序列长度

constmaxn=5000;

s:

i,j,k,n,max:

f:

=1tondoread(s[i]);

fillchar(f,sizeof(f),0);

f[1]:

max:

=2tondobegin

forj:

=1toi-1do

if(s[j]<

=s[i])and(f[i]<

f[j])thenf[i]:

=f[j];

f[i]:

=f[i]+1;

iff[i]>

maxthenmax:

=f[i];

writeln(max);

{最大不下降子序列数据:

3

123

ans=3}

//字符串处理

{输入仅一行:

该行包含若干个数据之间用半角逗号隔开

要求记录下每一个输入的数字,例:

389,207,155,300,299,170,158,65

procedureinit;

varpo2:

integer;

readln(a);

po2:

=pos('

'

a);

whilepo2<

0dobegin

inc(k);

val(copy(a,1,po2-1),s[k]);

a:

=copy(a,po2+1,length(a)-po2);

po2:

b

inc(k);

val(copy(a,1,length(a)),s[k]);

//init

str(i,s)过程:

将一个整数或实数i转换成字符串s;

copy(s,i,len);

pos(st1,st2):

求st1在st2的最初位置;

val(st,r):

将st转换成与r相同类型的值存入r中;

fillchar(s,sizeof(s),$f);

=1tondobegin

readln(a,b);

k:

=a;

work(b);

writeln(k);

{前向星简单优化的spfa}

var

a,b,e:

array[1..10000]oflongint;

vis:

array[1..20000]ofboolean;

q,d,f:

array[1..20001]oflongint;

n,m,i,s,t:

vari,j,x,y:

begin

j:

=a[(l+r)shr1];

whilea[i]<

whilea[j]>

ifnot(i>

j)thenbegin

y:

=a[i];

a[i]:

a[j]:

=y;

=b[i];

b[i]:

=b[j];

b[j]:

=e[i];

e[i]:

=e[j];

e[j]:

inc(i);

dec(j);

ifi<

rthenqsort(i,r);

//qsort

procedurespfa(s:

vari,k,head,tail:

fillchar(vis,sizeof(vis),0);

=1tondod[i]:

d[s]:

head:

tail:

q[1]:

=s;

vis[s]:

head:

=headmod10000+1;

k:

=q[head];

=f[k]tof[k+1]-1do

ifd[k]+e[i]<

d[b[i]]then

begin

d[b[i]]:

=d[k]+e[i];

ifnotvis[b[i]]thenbegin

tail:

=tailmod10000+1;

q[tail]:

vis[b[i]]:

vis[k]:

untilhead=tail;

//spfa

Begin

readln(n,m);

readln(a[i],b[i],e[i]);

qsort(1,m);

iff[a[i]]=0thenf[a[i]]:

f[n+1]:

=m+1;

=ndownto1do

iff[i]=0thenf[i]:

=f[i+1];

readln(s,t);

spfa(s);

writeln(d[t]);

end.

//高精乘法

constmaxn=10007;

i,j,k,l1,l2:

a,b,c,c1,c2,temp:

vars:

ansistring;

i:

readln(s);

l1:

=length(s);

=1tol1do

cases[i]of

0'

:

1'

2'

3'

=3;

4'

=4;

5'

=5;

6'

=6;

7'

=7;

8'

=8;

9'

=9;

//case

temp:

=1tol1doa[l1-i+1]:

=temp[i];

whilea[l1]=0dodec(l1);

l2:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 少儿英语

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1