基础代码汇总整理 for NOIP
- 格式:pdf
- 大小:99.33 KB
- 文档页数:30
●计算机语言计算机语言通常是一个能完整、准确和规则地表达人们的意图,并用以指挥或控制计算机工作的“符号系统”。
计算机语言通常分为三类:即机器语言,汇编语言和高级语言。
1、机器语言是用二进制代码表示的计算机能直接识别和执行的一种机器指令的集合。
它是计算机的设计者通过计算机的硬件结构赋予计算机的操作功能。
机器语言具有灵活、直接执行和速度快等特点。
2、为了克服机器语言难读、难编、难记和易出错的缺点,人们就用与代码指令实际含义相近的英文缩写词、字母和数字等符号来取代指令代码(如用ADD表示运算符号“+”的机器代码),于是就产生了汇编语言。
所以说,汇编语言是一种用助记符表示的仍然面向机器的计算机语言。
汇编语言亦称符号语言。
3、高级语言是面向用户的语言。
无论何种机型的计算机, 只要配备上相应的高级语言的编译或解释程序,则用该高级语言编写的程序就可以通用。
目前被广泛使用的高级语言有BASIC、PASCAL、C、COBOL、FORTRAN、LOGO 以及VC、VB等。
这些语言都是属于系统软件。
●计算机的主要性能指标1. 字长:在同一时间中处理二进制数的位数叫字长。
早期的微机字长一般是8位和16位,386以及更高的处理器大多是32位。
目前市面上的计算机的处理器大部分已达到64位。
2. 速度3. 存储系统容量(bit,B,KB,MB,GB,TB) 1B=8bit 1KB=1024B1MB(兆字节)=1024KB 1GB(兆兆字节)=1024MB 1TB=1024GB●计算机软件a、BIOS:"基本输入输出系统"。
其实,它是一组固化到计算机内主板上一个ROM芯片上的程序,它保存着计算机最重要的基本输入输出的程序、系统设置信息、开机后自检程序和系统自启动程序。
其主要功能是为计算机提供最底层的、最直接的硬件设置和控制。
解释程序:高级语言翻译的一种,它将源语言(如basic)书写的源程序作为输入,解释一句后就提交计算机执行一句,并不形成目标程序.翻译程序: (编译程序)一类很重要的语言处理程序,它把高级语言(如FORTRAN,COBOL,pascal,c等)源程序作为输入,进行翻译转换,产生出机器语言的目标程序,然后再让计算机去执行这个目标程序,得到计算结果.语言:机器语言汇编语言高级语言(面向对象,面向过程)数据库管理软件:Foxpro,Access,Orale,Sybase,DB2和Informix等。
{prog: 图结构name: 单源点最短路径edit: dijkstra}varn:longint;g:array[0..10,0..10]of longint;{ fillchar(g,sizeof(g),10) }{ 标记是否被更新过 }f:array[0..10]of boolean;{ 顶点i到start的最短距离 }d:array[0..10]of longint;procedure dijkstra(start:longint);var i,j,k,t,min:longint;beginfillchar(f,sizeof(f),10);d[start]:=0;f[start]:=false;for i:=1to n dod[i]:=g[start,i];for i:=1to n dobeginmin:=oo;for j:=1to n do{ 在“还没有被更新过”的集合中找最小值和它的位置 }if(f[j]and(d[j]<min))thenbeginmin:=d[j];k:=j;end;{ 加入“更新过”的集合 }f[k]:=false;{ 更新距离 }for j:=1to n doif f[j]thend[j]:=min(d[j],d[k]+g[k,j]);end;end;{prog: 图结构name: 单源点最短路径edit: bellman-ford}vare:array[1..maxe]of recorda,b,w:longint;end;{ 距源点s距离 }dis:array[1..maxn]of longint;{ 前驱 }pre:array[1..maxn]of longint;m,n,s:longint;procedure relax(u,v,w:longint);beginif dis[u]+w<dis[v]thenbegindis[v]:=dis[u]+w;pre[v]:=u;end;end;function bellman_ford:boolean;var i,j:longint;begin{ 每条边松弛 }for i:=1to n-1dofor j:=1to m dowith e[j]dorelax(a,b,w);{ 如果还能松弛 }for i:=1to m dowith e[i]doif dis[a]+w<dis[b]thenexit(false);exit(true)end;{prog: 图结构name: 单源点最短路径edit: spfa}procedure spfa(start:longint);var i,j,k,tv,te,head,tail:longint;beginfillchar(f,sizeof(f),0);fillchar(d,sizeof(d),10);q[1]:=start;f[start]:=true;d[start]:=0;head:=0;tail:=1;while head<tail dobegininc(head);te:=v[q[head]].start;for i:=1to v[q[head]].how dobegintv:=e[te].y;if(d[tv]>d[q[head]]+e[te].v)thenbegind[tv]:=d[q[head]]+e[te].v;if(f[tv]=false)thenbegininc(tail);q[tail]:=tv;f[tv]:=true;end;end;te:=e[te].next;end;f[q[head]]:=false;end;end;{prog: 图结构name: 最小生成树——prim算法edit: prim算法}var g:array[1..10,1..10]of longint;d:array[1..10]of longint;f:array[1..10]of boolean;procedure prim;var i,j,k,min:longint;beginfillchar(g,sizeof(g),0);fillchar(f,sizeof(f),0);for i:=1to n do d[i]:=g[1,i];f[1]:=true;for i:=2to n dobeginmin:=oo;for j:=1to n doif(f[j]=false)and(d[j]<min)thenbegin{这个边必须跨越两个集合}min:=d[j];k:=j;end;f[k]:=true;for j:=1to n do//修改距离if(not f[j])and(g[k,j]<d[j]))thend[j]:=g[k,j];end;end;{prog: 图结构name: 网络流求最大流edit: dfs}read(xx,yy,vv);{ 建正向边 }add(xx,yy,vv);{ 建退流边 }add(yy,xx,0);procedure search(root,flow:longint);var te,tv:longint;beginvis[root]:=true;if(root=n)thenbegin{ 更新答案 }ans:=ans+flow;{ 更新退流信息 }rflow:=flow;{ 标记“搜索到了增广路” }done:=false;exit;end;{ 递归搜索 }te:=v[root].start;while(te<>0)dobegintv:=e[te].y;if(not vis[tv])and(e[te].v>0)thenbeginsearch(tv,min(flow,e[te].v));{ 如果找到了增广路 }if not done thenbegin{ 退流 }dec(e[te].v,rflow);{ 正向边编号肯定能是奇数 }{ 退流操作 }if odd(te)theninc(e[te+1].v,rflow)elseinc(e[te-1].v,rflow);exit;end;end;te:=e[te].next;end;end;beginans:=0;indata;while not done dobeginfillchar(vis,sizeof(vis),0);done:=true;search(1,maxlongint);end;writeln(ans);end.{prog: 堆name: 堆的相关操作edit: 小根堆}{上移}procedure heapup(root:longint);beginwhile(root>1)dobeginif a[root]<a[root shr1]thenswap(a[root],a[root shr1])elsebreak;root:=root shr1;end;end;{下移}procedure heapdown(root:longint); beginwhile(root*2<=n)dobeginroot:=root shl1;if(root+1<=n)and(a[root+1]<a[root])theninc(root);if(a[root shr1]>a[root])thenswap(a[root shr1],a[root])elsebreak;end;end;{插入}procedure heapinsert(x:longint);begininc(n);a[n]:=x;heapup(n);end;{删除}procedure heapdelete(root:longint);begina[root]:=a[n];dec(n);if(root<=n)thenbeginif(a[root]<a[root shr1])and(root>1)thenheapup(root)elseheapdown(root);end;end;{建堆}procedure heapmake;var i:longint;beginfor i:=(n shr1)downto1doheapdown(i);end;{prog: 堆name: 堆的相关操作edit: 大根堆}{上移}procedure heapup(root:longint);var t:longint;beginwhile(root>1)dobegint:=root shr1;if a[root]>a[t]thenbeginswap(b[t],b[root]);swap(c[b[t]],c[b[root]]);swap(a[t],a[root]);endelsebreak;root:=t;end;end;{下移}procedure heapdown(root:longint);var t:longint;beginwhile(root*2<=n)dobegint:=root;root:=t shl1;if(root+1<=n)and(a[root+1]>a[root])theninc(root);if(a[t]<a[root])thenbeginswap(b[t],b[root]);swap(c[b[t]],c[b[root]]);swap(a[t],a[root]);endelsebreak;end;end;{更改}procedure heapedit(root,v:longint);begina[root]:=v;if((a[root]>a[root shr1])and(root>1)) thenheapup(root)elseheapdown(root);end;{prog: 线段树name: 线段树的相关操作edit: 线段树}{ 线段树查询区间最大值并进行操作 }{ request 是要操作的量 }var n,left,right,lim,request:longint;s:array[0..250000]of recorddev,data:longint;end;function max(x,y:longint):longint;begin if(x>y)then exit(x);exit(y);end; procedure downdev(root:longint);var dev:longint;begin{ 取得偏移量 }dev:=s[root].dev;{ 下传偏移量 }inc(s[root*2].data,dev);inc(s[root*2].dev,dev);inc(s[root*2+1].data,dev);inc(s[root*2+1].dev,dev);{ 清空本节点的偏移量 }s[root].dev:=0;end;procedure updata(l,r,root:longint);var mid:longint;begin{ 超出边界 }if(right<l)or(r<left)then exit;{ 能包含 }if(left<=l)and(right>=r)thenbegin{ 修改值是这样修改的~ }inc(s[root].data,request);inc(s[root].dev,request);exit;end;{ 下传偏移量 }downdev(root);{ 更新本节点 }mid:=(l+r)shr1;updata(l,mid,root*2);updata(mid+1,r,root*2+1);s[root].data:=max(s[root*2].data,s[root*2+1].data);end;function find(l,r,root:longint):longint;var mid:longint;beginif(right<l)or(r<left)then exit(-oo);if(left<=l)and(right>=r)thenexit(s[root].data);{ 下传偏移量 }downdev(root);{ 返回数据 }mid:=(l+r)shr1;exit(max(find(l,mid,root*2),find(mid+1,r,root*2+1)));end;{prog: 图结构name: 强连通分量edit: tarjan}var{left表示点 root 没离开栈 vis表示点 root 有没有被访问过}i,n,m,now,time,color,top:longint;v:array[0..10001]of recordstart:longint;end;e:array[0..100001]of recordy,next:longint;end;dfn,low,stack,encolor:array[0..10001]oflongint;vis,left:array[0..10001]of boolean;procedure indata;var i,xx,yy,op:longint;begintime:=0;color:=0;top:=0;fillchar(e,sizeof(e),0);fillchar(v,sizeof(v),0);fillchar(dfn,sizeof(dfn),0);fillchar(low,sizeof(low),0);fillchar(vis,sizeof(vis),0);fillchar(left,sizeof(left),1);fillchar(encolor,sizeof(encolor),0);end;procedure tarjan(root:longint);var te,tv,ttimes:longint;begininc(time);dfn[root]:=time;low[root]:=time;inc(top);stack[top]:=root;{点 root 入栈 }left[root]:=false;vis[root]:=true;te:=v[root].start;while(te<>0)dobegintv:=e[te].y;if not left[tv]thenlow[root]:=min(low[root],dfn[tv]);if not vis[tv]thenbegintarjan(tv);low[root]:=min(low[root],low[tv]);end;te:=e[te].next;end;if(dfn[root]=low[root])thenbegininc(color);tv:=0;while(tv<>root)dobegintv:=stack[top];dec(top);encolor[tv]:=color;left[tv]:=true;end;end;end;{ prog: 图结构name: 割点edit: dfn_low}var n,time,degree:longint;v:array[0..100001]of recordstart:longint;end;e:array[0..300001]of recordy,next:longint;end;dfn,low:array[0..100001]of longint;yes:array[0..100001]of boolean;procedure indata;var xx,yy:longint;beginans:=0;time:=0;degree:=0;fillchar(e,sizeof(e),0);fillchar(v,sizeof(v),0);fillchar(dfn,sizeof(dfn),0);fillchar(low,sizeof(low),0);fillchar(yes,sizeof(yes),0);end;procedure search(root:longint);var te,tv:longint;begin{标记时间戳}inc(time);dfn[root]:=time;low[root]:=time;te:=v[root].start;while(te<>0)dobegintv:=e[te].y;{这条边是树边}if(dfn[tv]=0)thenbegin{记录 root 在搜索树中的度}if root=1then inc(degree);{递归}search(tv);{ tv 是 root 的儿子,更新}low[root]:=min(low[root],low[tv]);{判断割点}if low[tv]>=dfn[root]then这个点是割点end{返祖边}elselow[root]:=min(low[root],dfn[tv]);te:=e[te].next;end;end;调用:枚举,如果没有被搜索即调用degree:=0;if(degree>1)then yes[1]:=trueelse yes[1]:=false;{prog: 图结构name: 桥edit: dfn_low}将上面代码中的{ 判断割点 }下面的等于号去掉{prog: lisname: 最长上升子序列edit: 二分优化}functiondichotomy(l,r,aim:longint):longint;var mid:longint;begin{找出小于等于 aim 的最大编号}while(l+1<r)dobeginmid:=(l+r)shr1;if b[mid]<=aim then l:=midelse r:=mid;end;if(b[r]<=aim)then exit(r);exit(l); end;procedure lis;var i,t,ans,now:longint;beginfillchar(f,sizeof(f),0);fillchar(b,sizeof(b),10);now:=1;ans:=1;f[1]:=1;b[1]:=a[1];for i:=2to n dobegint:=dichotomy(0,now,a[i])+1;f[i]:=max(f[i],t);now:=max(now,t);ans:=max(ans,f[i]);if a[i]<=b[f[i]]then b[f[i]]:=a[i];end;writeln(ans);end;{prog: 动态规划name: 最长不下降子序列edit: 二分优化}functiondichotomy(l,r,aim:longint):longint;var mid:longint;begin{找出小于 aim 的最大编号}while(l+1<r)dobeginmid:=(l+r)shr1;if b[mid]<aim then l:=midelse r:=mid;end;if(b[r]<aim)then exit(r);exit(l);end;procedure lis;var i,t,ans,now:longint;beginfillchar(f,sizeof(f),0);fillchar(b,sizeof(b),10);now:=1;ans:=1;f[1]:=1;b[1]:=a[1];for i:=2to n dobegint:=dichotomy(0,now,a[i])+1;f[i]:=max(f[i],t);now:=max(now,t);ans:=max(ans,f[i]);if a[i]<b[f[i]]then b[f[i]]:=a[i];end;writeln(ans);end;{prog: 动态规划name: 选课edit: 树形dp}constfn='course';inf=fn+'.in';ouf=fn+'.out';varn,m:longint;tree:array[0..1001]of recordvalue,left,right:longint;end;f:array[0..1001,0..1001]of longint;choose:array[0..1001]of boolean;function max(x,y:longint):longint;begin if(x>y)then exit(x);exit(y);end;procedure add(xx,yy,vv:longint);begin{让“超级根” 等于 n+1 }{因为在多叉转成的二叉树里面, 0 代表“没有”}if(xx=0)then xx:=n+1;{本节课的价值}tree[yy].value:=vv;{多叉树转换为二叉树}tree[yy].right:=tree[xx].left;tree[xx].left:=yy;end;procedure indata;var xx,yy,vv:longint;beginfillchar(tree,sizeof(tree),0);fillchar(choose,sizeof(choose),0);readln(n,m);{因为要选“超级根”,所以会“多选一门课”}inc(m);for yy:=1to n dobegin{读入先修课(xx)和本节课价值(vv)}read(xx,vv);add(xx,yy,vv)end;end;procedure dp(root,how:longint);var i:longint;begin{如果这个点是“没有”,或者在这个树上不选,那么再做下去就没有意义}if((root=0)or(how=0))then exit;{这种状态已经被处理过了}if(f[root,how]<>0)then exit;for i:=1to how dobegin{处理左孩子}dp(tree[root].left,i);{处理右兄弟}dp(tree[root].right,i);end;{只选择兄弟,不用考虑自己}f[root,how]:=f[tree[root].right,how];{如果要在自己上面选择的话}for i:=0to how-1do{自己 + 左孩子选 i 个 + 右孩子选 how-i-1 个}f[root,how]:=max(f[root,how],tree[root]. value+f[tree[root].left,i]+f[tree[root].r ight,how-i-1]);end;procedure oudata;var i:longint;procedure road(root,how:longint);var i:longint;begin{根上面一样的原因}if((root=0)or(how=0))then exit;{如果在这个点没有选择根}if(f[root,how]=f[tree[root].right,how])thenroad(tree[root].right,how)elsefor i:=0to how-1doif(tree[root].value+f[tree[root].left,i]+f[tree[root].right,how-i-1]=f[root,how])thenbeginchoose[root]:=true;road(tree[root].left,i);road(tree[root].right,how-i-1);end;end;beginwriteln(f[n+1,m]);{在已经知道了最大值的情况下,找出方案}road(n+1,m);{输出方案}for i:=1to n doif choose[i]then writeln(i);end;begin randomize;indata;{因为 0 是表示“没有”,可能会导致树里面出现好多0 ,所以让根等于 n+1 }dp(n+1,m);oudata;end.{prog: 动态规划name: 最大利润edit: 树形dp}vartree:array[0..100001]of recordstart,data:longint;end;edge:array[0..200001]of recordy,next:longint;end;fop,fno:array[0..100001]of longint;{fop 表示在以 i 为根的树上开饭店,能得到的最大价值( i 这个点一定要开)}{fno 表示在以 i 为根的树上开饭店,能得到的最大价值( i 这个点一定不开)}procedure indata;var i,xx,yy:longint;beginfillchar(fop,sizeof(fop),0);fillchar(fno,sizeof(fno),0);fillchar(tree,sizeof(tree),0);end;function dp(root,father:longint):longint;varte,sumop,sumno:longint;{ sumno 是不取 root 的时候能得到的最大价值}{ sumop 是取 root 的时候能得到的最大价值}begin{边界:已经被搜索过了}if(fop[root]<>0)or(fno[root]<>0)thenexit(max(fop[root],fno[root]));{否则的话……}sumop:=0;sumno:=0;te:=tree[root].start;while(te<>0)dobegin{如果不取root ,则它的子结点都要取最大值并相加}if(edge[te].y)<>father thensumno:=sumno+dp(edge[te].y,root);{如果取root ,则它的子结点都不能取}sumop:=sumop+fno[edge[te].y];{……继续……}te:=edge[te].next;end;sumop:=sumop+tree[root].data;fop[root]:=sumop;fno[root]:=sumno;exit(max(sumop,sumno));end;beginindata;now:=random(n);if(now=0)then now:=n-random(n);writeln(dp(now,now));end.{prog: 动态规划name: 石子合并edit: 区间dp}varn:longint;f:array[0..301,0..301]of longint;{起点为 i ,终点为 j 的合并}sum:array[0..301]of longint;procedure indata;var i,t:longint;beginfillchar(f,sizeof(f),10);sum[0]:=0;read(n);for i:=1to n dobeginread(t);sum[i]:=sum[i-1]+t;f[i,i]:=0;end;end;procedure dp;var p,i,j,k:longint;beginfor p:=1to n do{枚举区间长度}for i:=1to n do{枚举起点}beginj:=i+p-1;{终点,可直接算出来}if(j>n)then break;{终点大于限度,退出}for k:=i to j-1do{枚举从哪断开}f[i,j]:=min(f[i,j],f[i,k]+f[k+1,j]+sum[j]-sum[i-1]);end;end;beginindata;dp;writeln(f[1,n]);end.{prog: 字符串name: kmpedit: 字符串}procedure makenext(s:string);var i,j:longint;beginnext[1]:=0;j:=0;for i:=2to length(s)dobeginwhile(j>0)and(s[j+1]<>s[i])doj:=next[j];if s[j+1]=s[i]then inc(j);next[i]:=j;end;end;procedure kmp(a,b:string;value:longint);var i,j,longa,longb:longint;begin{取得字符串长度}longa:=length(a);longb:=length(b);j:=0;for i:=1to longa dobeginwhile((j>0)and(b[j+1]<>a[i]))doj:=next[j];if b[j+1]=a[i]then inc(j);{如果找到了匹配}if j=longb thenbegin{更新答案}inc(ans,value*(i-longb+1));{为了让搜索能够继续下去,找完所有匹配}j:=next[j];end;end;end;{prog: 字符串name: 单词查找树edit: 单词查找树}trie:array[0..800000]of records:array['a'..'z']of longint;end;ocedure addtrie(deep,now:longint);{已经到了第 deep 个字母}{已经到了第 now 个节点}var temp:char;begin{递归边界:深度小于等于字符串长度}if(deep>long)then exit;temp:=s[deep];{如果现在长度为 deep-1 的前缀在树中,但长度为 deep 的单词不在树中}if(trie[now].s[temp]=0)thenbegin{新申请一个空间}inc(n);{类似于邻接表}trie[now].s[temp]:=n;end;{递归建树}addtrie(deep+1,trie[now].s[temp]); end;。
python最基础代码Python是一种简单易学且功能强大的编程语言,被广泛应用于各个领域。
本文将为您介绍Python的最基础代码,帮助初学者快速上手。
1. 变量与数据类型Python中的变量不需要声明,可以直接赋值。
下面是一些常用的数据类型及其定义方法:- 整数类型(int):用来表示整数,如x = 10- 浮点数类型(float):用来表示带有小数的数值,如y = 3.14- 字符串类型(str):用来表示文本信息,如name = "John"- 布尔类型(bool):用来表示真(True)或假(False),如is_valid = True2. 算术运算符Python支持常用的算术运算符,包括加法(+)、减法(-)、乘法(*)、除法(/)和取余(%)。
以下是一些示例代码:```pythona = 10b = 3print(a + b) # 输出:13print(a - b) # 输出:7print(a * b) # 输出:30print(a / b) # 输出:3.3333333333333335print(a % b) # 输出:1```3. 条件语句条件语句用于根据条件的不同执行不同的代码块。
Python中的条件语句使用if、elif和else关键字。
以下是一个简单的示例:```pythonage = 18if age >= 18:print("您已成年")elif age >= 12:print("您是青少年")else:print("您是儿童")```4. 循环语句循环语句用于重复执行特定的代码块。
Python中的循环有两种形式:for循环和while循环。
以下是一个示例:for i in range(1, 6):print(i) # 输出:1 2 3 4 5count = 0while count < 5:print(count) # 输出:0 1 2 3 4count += 1```5. 函数定义函数是一段可重用的代码块,用于执行特定的任务。
部分NOIP基础模块代码总结--by Evan1004一、数据结构1、哈希表procedure hash_a(x:longint);vart:longint;begint:=x mod 399997;if first[t]=0 thenbegininc(tot1);first[t]:=tot1;a[tot1].x:=x;a[tot1].zhi:=0;end elsebegint:=first[t];while (a[t].zhi>0) and (a[t].x<>x) dot:=a[t].zhi;if a[t].zhi=0 thenbegininc(tot1);a[tot1].x:=x;a[tot1].zhi:=0;a[t].zhi:=tot1;end;end;end;2、堆(代码为大根堆操作,小根堆只需将a[i]>a[j]改为a[i]<a[j]即可) 1).插入(上调)procedure up(i);beginwhile i>1 dobeginj:=i div 2;if a[i]>a[j] thenbeginswap(a[i],a[j]);i:=j;endelsebreak;end;end;2).删除(下调)procedurd down(i,n:longint);beginwhile i<=n shr 1 do //因为a[n shr 1]的儿子就是a[n]了beginj:=2*i;if (j+1<=n) and (a[j+1]>a[j]) then //选择两个儿子中的较大者j:=j+1;if a[j]>a[i] thenbeginswap(a[i],a[j]);i:=j;endelsebreak;end;end;3、线段树1.建树procedure start(l,r:longint; var t:longint);varm:longint;beginm:=(l+r) div 2 ;inc(tot);t:=tot;a[t].l:=l;a[t].r:=r;if l<r then beginstart(l,m,a[t].lc);start(m+1,r,a[t].rc);end;end;2.修改区间procedure insert(t,l,r:longint);beginif (a[t].l=l) and (a[t].r=r) thenbegin inc(a[t].data); exit; end;if (r<=a[a[t].lc].r) then insert(a[t].lc,l,r)else if l>=a[a[t].rc].l then insert(a[t].rc,l,r)else begininsert(a[t].lc,l,a[a[t].lc].r);insert(a[t].rc,a[a[t].rc].l,r);end;end;3.修改点procedure insert(t,x:longint);beginif t=0 then exit;if (x=a[t].l) and (x=a[t].r) then begin inc(a[t].data);exit;end;if (x>=a[t].l) and (x<=a[t].r) thenbegininc(a[t].data);insert(a[t].lc,x);insert(a[t].rc,x);end;end;4.查询点procedure find(t,x:longint);beginif t=0 then exit;if (x=a[t].l) and (x=a[t].r) then begin inc(ans,a[t].data); exit;end;if (x>=a[t].l) and (x<=a[t].r) thenbegininc(ans,a[t].data);find(a[t].lc,x);find(a[t].rc,x);end;4.第K小(大)function find(t,x:longint):longint;beginif t=0 then exit;if (a[t].l=a[t].r) then find:=a[t].lelse if a[a[t].lc].data>=x then find:=find(a[t].lc,x)else find:=find(a[t].rc,x-a[a[t].lc].data);5.最大(小)function find(t,x,y:longint):longint;beginif t=0 then exit;if (a[t].l=x) and (a[t].r=y) then exit(a[t].data) elseif y<=a[a[t].lc].r then exit(find(a[t].lc,x,y)) elseif x>=a[a[t].rc].l then exit(find(a[t].rc,x,y)) elseexit(min(find(a[t].lc,x,a[a[t].lc].r),find(a[t].rc,a[a[t].rc].l,y)));end;4、二叉搜索树二叉搜索树procedure insert(var t,x:longint);beginif t=0 thenbegininc(tot);t:=tot;tree[t].x:=x;tree[t].lc:=0;tree[t].rc:=0;tree[t].time:=1;end elsebeginif x>tree[t].x then insert(tree[t].rc,x);if x<tree[t].x then insert(tree[t].lc,x);if x=tree[t].x then inc(tree[t].time);end;end;//======================================================== procedure print(t:longint);beginif t=0 then exit;print(tree[t].lc);writeln(tree[t].x,' ',tree[t].time);print(tree[t].rc);end;二、图论1、最小生成树1.prim算法procedure prim;vari,j,min,minj:longint;beginv[1]:=0;for i:=1 to n dobeginmin:=maxlongint;for j:=1 to n doif (b[j]=0) and (v[j]<min) thenbeginmin:=v[j];minj:=j;end;sum:=sum+min;b[minj]:=1;for j:=1 to n doif (b[j]=0)and(a[minj,j]>0)and(v[j]>a[minj,j]) thenv[j]:=a[minj,j];end;writeln(sum);end;2.Kruskal算法(算法过程较多,引一道纯最小生成树题举例)constmaxn=1000000;vari,j,k,m,n,p,q,x,y:longint;f,u,v:array[1..maxn] of longint; ans,s:extended;e:array[1..maxn] of real; procedure swap(var x,y:longint); vart:longint;begint:=x;x:=y;y:=t;end;procedure qsort(l,r:longint);vari,j:longint;x,y:extended;begini:=l;j:=r;x:=e[random(r-l)+l];repeatwhile e[i]<x do inc(i);while e[j]>x do dec(j);if i<=j thenbeginy:=e[i];e[i]:=e[j];e[j]:=y;swap(u[i],u[j]);swap(v[i],v[j]);inc(i);dec(j);end;until i>j;if l<j then qs(l,j);if i<r then qs(i,r);end;function get(x:longint):longint; beginif f[x]=0 then exit(x);if f[f[x]]=0 then exit(f[x]);f[x]:=get(f[x]);exit(f[x]);end;procedure init;beginfillchar(e,sizeof(e),0);fillchar(u,sizeof(u),0);fillchar(v,sizeof(v),0);fillchar(f,sizeof(f),0);readln(s);readln(n);m:=0;while not eof dobegininc(m);readln(u[m],v[m],e[m]);end;end;procedure outit;beginif (k=n-1) and (ans<=s) thenwriteln('Need ',ans:0:2,' miles of cable') elsewriteln('Impossible');end;begininit;qsort(1,m);// Kurskalk:=0;for i:=1 to m dobeginp:=get(u[i]);q:=get(v[i]);if p<>q thenbeginans:=ans+e[i];f[p]:=q;inc(k);end;end;outit;end.2、最短路问题1.Floyd (简单,但是雷达不动的o(n3)...)for k:=1 to 52 dofor i:=1 to 52 dofor j:=1 to 52 doif a[i,k]+a[k,j]<a[i,j] thena[i,j]:=a[i,k]+a[k,j];2.Dijkstraprocedure Dijkstra;vari,j,p:integer;min:longint;beginp:=0;fillchar(v,sizeof(v),100);fillchar(b,sizeof(b),false);v[m]:=0;for i:= 1 to n dobeginmin:=maxlongint;for j:= 1 to n doif (min>v[j])and(not b[j]) thenbeginmin:=v[j];p:=j;end;b[p]:=true;for j:= 1 to n doif (not b[j])and(v[j]>a[p,j]+v[p]) thenv[j]:=a[p,j]+v[p];end;for i:= 1 to n dowriteln(v[i]);end;3.SPFAprocedure spfa;beginfillchar(q,sizeof(q),0); h:=0; t:=0;//队列fillchar(b,sizeof(b),false);//b[i]判断i是否在队列中for i:=1 to n dov[i]:=maxint;//初始化最小值inc(t);q[t]:=1;b[1]:=true;v[1]:=0;//这里把1作为源点while h<>t dobeginh:=(h mod n)+1;x:=q[h];b[x]:=false;for i:=1 to n doif (a[x,i]>0) and (v[x]+a[x,i]<v[i]) thenbeginv[i]:=v[x]+a[x,i];if not(b[i]) thenbegint:=(t mod n)+1;q[t]:=i;b[i]:=true;end;end;end;end;三、不知道分什么类1、最大公约数:function gcd(n,m:longint):longint;beginif m=0 then exit(n);gcd:=gcd(m,n mod m);end;2、最小公倍数:lcm(n,m):=n*m/gcd(n,m) //先除再乘,不然超LONGINT了3、快速排序procedure qsort(l,r:longint);vari,j,k,mid:longint;begini:=l;j:=r;mid:=a[(l+r) shr 1];repeatwhile a[i]<mid do inc(i);while a[j]>mid do dec(j);if i<=j thenbegink:=a[i]; a[i]:=a[j];a[j]:=k;inc(i);dec(j);end;until i>j;if i<r then qsort(i,r);if l<j then qsort(l,j);end;4、并查集1.判断是否在同一并查集内function get(x:longint):longint; beginif f[x]=x then exit(x) elsebeginf[x]:=get(f[x]); \\捎带路径压缩exit(f[x]);end;end;2.合并procedure union(x,y:longint); beginif get(x)<>get(y) thenf[get(x)]:=get(y);end;深搜的基本框架procedure search;varif 结束条件1 thenbeginif 结束条件2 then inc(ans);exit;end;{ 计算、操作}for i:=1 to n do //n为搜索宽度search(n);end;。
C语言入门基础代码(20条案例)下面是20条基础案例:1. 输出Hello, World!#include <stdio.h> // 使用标准输入输出库int main() {printf("Hello, World!\n"); // 输出字符串return 0; // 返回程序执行成功}2. 判断一个数是否为偶数#include <stdio.h> // 使用标准输入输出库int main() {int num; // 定义变量printf("请输入一个整数:");scanf("%d", &num); // 从控制台输入一个整数if(num % 2 == 0) { // 如果余数为0说明是偶数printf("%d 是偶数\n", num);} else {printf("%d 是奇数\n", num);}return 0;}3. 计算两个数的和#include <stdio.h> // 使用标准输入输出库int main() {int a, b; // 定义变量int sum; // 定义变量printf("请输入两个整数:");scanf("%d%d", &a, &b); // 从控制台输入两个整数sum = a + b; // 求和printf("%d + %d = %d\n", a, b, sum); // 输出结果return 0;}4. 求一个数的平方#include <stdio.h> // 使用标准输入输出库int main() {int num; // 定义变量int square; // 定义变量printf("请输入一个整数:");scanf("%d", &num); // 从控制台输入一个整数square = num * num; // 求平方printf("%d 的平方是%d\n", num, square); // 输出结果return 0;}5. 判断一个字符是否为数字#include <stdio.h> // 使用标准输入输出库#include <ctype.h> // 使用字符函数库int main() {char ch; // 定义变量printf("请输入一个字符:");scanf("%c", &ch); // 从控制台输入一个字符if(isdigit(ch)) { // 判断是否为数字printf("%c 是数字\n", ch);} else {printf("%c 不是数字\n", ch);}return 0;}6. 计算数组元素的平均值#include <stdio.h> // 使用标准输入输出库int main() {int arr[] = {1, 2, 3, 4, 5}; // 定义数组int len = sizeof(arr) / sizeof(int); // 数组长度int sum = 0; // 定义变量int avg; // 定义变量for(int i = 0; i < len; i++) { // 遍历数组sum += arr[i]; // 累加求和}avg = sum / len; // 求平均值printf("数组的平均值是%d\n", avg); // 输出结果return 0;}7. 按照下标访问数组元素#include <stdio.h> // 使用标准输入输出库int main() {int arr[] = {1, 2, 3, 4, 5}; // 定义数组int len = sizeof(arr) / sizeof(int); // 数组长度for(int i = 0; i < len; i++) { // 遍历数组printf("arr[%d] = %d\n", i, arr[i]); // 输出每个元素}return 0;}8. 使用指针访问数组元素#include <stdio.h> // 使用标准输入输出库int main() {int arr[] = {1, 2, 3, 4, 5}; // 定义数组int len = sizeof(arr) / sizeof(int); // 数组长度int *p = arr; // 把数组首地址赋给指针变量for(int i = 0; i < len; i++) { // 遍历数组printf("arr[%d] = %d\n", i, *(p + i)); // 输出每个元素}return 0;}9. 求Fibonacci 数列的第n 项#include <stdio.h> // 使用标准输入输出库int main() {int n; // 定义变量int a = 0, b = 1, c; // 定义变量printf("请输入一个正整数:");scanf("%d", &n); // 从控制台输入一个整数for(int i = 1; i <= n; i++) { // 求Fibonacci 数列的第n 项c = a + b;a = b;b = c;}printf("Fibonacci 数列的第%d 项是%d\n", n, a); // 输出结果return 0;}10. 使用递归计算阶乘#include <stdio.h> // 使用标准输入输出库int factorial(int n) { // 定义递归函数if(n == 0 || n == 1) {return 1;} else {return n * factorial(n - 1);}}int main() {int n; // 定义变量printf("请输入一个非负整数:");scanf("%d", &n); // 从控制台输入一个整数int result = factorial(n); // 调用递归函数计算阶乘printf("%d 的阶乘是%d\n", n, result); // 输出结果return 0;}11. 判断一个数是否是质数#include <stdio.h>#include <stdbool.h>bool isPrime(int num) {if(num <= 1) {return false; // 小于等于1的数都不是质数}for(int i = 2; i * i <= num; i++) { // 只要从2到根号num遍历就可以了if(num % i == 0) {return false; // 如果存在因子,则不是质数}}return true;}int main() {int num;printf("请输入一个整数:");scanf("%d", &num);bool result = isPrime(num); // 调用isPrime函数if(result) {printf("%d 是质数\n", num);} else {printf("%d 不是质数\n", num);}return 0;}12. 计算圆的面积和周长#include <stdio.h>const double PI = 3.1415926;int main() {double r, area, perimeter;printf("请输入圆的半径:");scanf("%lf", &r);area = PI * r * r; // 计算面积perimeter = 2 * PI * r; // 计算周长printf("圆的面积是%.2f,周长是%.2f\n", area, perimeter);return 0;}13. 计算斐波那契数列的前n 项#include <stdio.h>int main() {int n;printf("请输入要输出的斐波那契数列项数:");scanf("%d", &n);int a = 0, b = 1, c; // 定义三个变量for(int i = 1; i <= n; i++) { // 输出前n项斐波那契数列printf("%d ", a);c = a + b;a = b;b = c;}printf("\n"); // 换行return 0;}14. 嵌套循环输出九九乘法表#include <stdio.h>int main() {for(int i = 1; i <= 9; i++) { // 控制行数for(int j = 1; j <= i; j++) { // 控制列数printf("%d*%d=%-2d ", j, i, i * j); // 左对齐输出}printf("\n"); // 换行}return 0;}15. 获得数组的最大值和最小值#include <stdio.h>int main() {int arr[] = {3, 5, 8, 1, 4, 9, 6, 2, 7};int len = sizeof(arr) / sizeof(int);int max = arr[0], min = arr[0]; // 假设第一个元素既是最大值也是最小值for(int i = 1; i < len; i++) {if(arr[i] > max) { // 更新最大值max = arr[i];}if(arr[i] < min) { // 更新最小值min = arr[i];}}printf("数组的最大值是%d,最小值是%d\n", max, min);return 0;}16. 判断一个数是否为回文数```c#include <stdio.h>#include <stdbool.h>bool isPalindrome(int num) {if(num < 0) { // 负数不是回文数return false;}int temp = num, reversed = 0; // 定义需要用到的变量while(temp != 0) { // 反转整数reversed = reversed * 10 + temp % 10;temp /= 10;}return (num == reversed); // 如果反转后等于原来的数,则为回文数}int main() {int num;printf("请输入一个整数:");scanf("%d", &num);bool result = isPalindrome(num);if(result) {printf("%d 是回文数\n", num);} else {printf("%d 不是回文数\n", num);}return 0;}17. 将字符串反转输出#include <stdio.h>#include <string.h>int main() {char str[100];printf("请输入一个字符串:");scanf("%s", str);int len = strlen(str);for(int i = len - 1; i >= 0; i--) { // 倒序输出printf("%c", str[i]);}printf("\n"); // 换行return 0;}18. 将一个二维数组按列排序#include <stdio.h>void sortCols(int arr[][3], int rows) {for(int j = 0; j < 3; j++) { // 按列排序for(int i = 0; i < rows - 1; i++) {for(int k = i + 1; k < rows; k++) {if(arr[i][j] > arr[k][j]) { // 比较大小并交换int temp = arr[i][j];arr[i][j] = arr[k][j];arr[k][j] = temp;}}}}}int main() {int arr[][3] = {{2, 5, 9}, {7, 6, 1}, {4, 3, 8}};int rows = sizeof(arr) / sizeof(arr[0]); // 计算数组的行数sortCols(arr, rows); // 调用函数排序for(int i = 0; i < rows; i++) { // 输出排序后的数组for(int j = 0; j < 3; j++) {printf("%d ", arr[i][j]);}printf("\n"); // 换行}return 0;}19. 判断一个字符串是否为回文串#include <stdio.h>#include <string.h>#include <stdbool.h>bool isPalindrome(char str[]) {int len = strlen(str);for(int i = 0; i < len / 2; i++) { // 判断左右字符是否一样if(str[i] != str[len - i - 1]) {return false;}}return true;}int main() {char str[100];printf("请输入一个字符串:");scanf("%s", str);bool result = isPalindrome(str); // 调用函数判断是否为回文串if(result) {printf("%s 是回文串\n", str);} else {printf("%s 不是回文串\n", str);}return 0;}20. 将一个整数转换成二进制数并输出#include <stdio.h>void decToBin(int num) {if(num > 1) { // 递归调用decToBin(num / 2);}printf("%d", num % 2); // 每次输出余数}int main() {int num;printf("请输入一个十进制数:");scanf("%d", &num);printf("%d 的二进制数为", num);decToBin(num); // 调用函数输出二进制数printf("\n"); // 换行return 0;}。
OK备战NO IP 2010提高组初赛复习——程序设计基础篇第一章简单程序 (2)第一节Pascal程序结构和基本语句 (2)第二节顺序结构程序少基本数据类型 (5)第二章分支程序 (6)第一节条件语句与复合语句 (6)第二节情况语句与算术标准函数 (8)第三章循环程序 (10)第一节for循环 (10)笫二节Repeat 循环 (14)第三节While循坏 (16)第四章函数与过程 (17)第一节函数 (17)第二节自定义过程 (19)第五章Pascal的自定义数据类型 (20)第一节数组与子界类型 (20)第二节二维数组 (23)第一章简单程序程序设计语言,是一组用来定义计算机程序的语法规则。
它是一种被标准化的交流技巧,用来向计算机发出指令。
按语言级别有低级语言和高级语言Z分。
低级语言包括机器语言和汇编语言。
它的特点是与特定的机器有关,功效高,但使用复杂、繁琐、费吋、易出差错。
高级语言的表示方法要比低级语言更接近于待解问题的表示方法,其特点是在一定程度上与具体机器无关,易学、易用、易维护。
由于当高级语言程序翻译成相应的低级语言程序时,一般说來,i个高级语言程序单位要对应多条机器指令,相应的编译程序所产生的目标程序往往功效较低。
(例:Pascal、C、C++、Java等)第一节Pascal程序结构和基本语句在未系统学习Pascal语言Z前,材且绕过那些繁琐的语法规则细节,通过下面的简单例题,初步掌握Pascal程序的基本组成和基本语句的用法。
[例1.1]编程在屏幕上显示“Hello World!”。
Pascal 程序:Program exll;BeginWritelnCHello World!1);Rcadln;End.这个简单样例程序,希望大家的程序设计学习能有一个良好的开端。
程序中的Writein是一个输出语句,它能命令计算机在屏幕上输出相应的内容,而紧跟Writein语句后是一对圆括号,其小用单引号引起的部分将被原原本本地显示出來。
完善程序题总结归纳By:七(6) yx一、【题目】(哥德巴赫猜想)哥德巴赫猜想是指,任一大于2的偶数都可写成两个质数之和。
迄今为止,这仍然是一个著名的世界难题,被誉为数学王冠上的明珠。
试编写程序,验证任一大于2且不超过n的偶数都能写成两个质数之和。
#include<iostream>using namespace std;int main(){const int SIZE=1000;int n,r,p[SIZE],i,j,k,ans;bool tmp;cin>>n;r=1;p[1]=2;for(i=3;i<=n;i++){①;for(j=1;j<=r;j++)if(i% ②==0){tmp=false;break;}if(tmp){r++;③;}}ans=0;for(i=2;i<=n/2;i++){tmp=false;for(j=1;j<=r;j++)for(k=j;k<=r;k++)if(i+i== ④ ){tmp=true;break;}if(tmp)ans++;}cout<<ans<<endl;return 0;}若输入n为2010,则输出⑤时表示验证成功,即大于2且不超过2010的偶数都满足哥德巴赫猜想。
【算法】先for一遍,找出质数,然后对每一个偶数进行一一匹配(2除外),效率O(n^3)【代码】1、tmp=1;2、p[j];3、p[r]=j;4、p[j]+p[k]5、1004【年份】2010年二、【题目】(过河问题) 在一个月黑风高的夜晚,有一群人在河的右岸,想通过唯一的一根独木桥走到河的左岸.在伸手不见五指的黑夜里,过桥时必须借照灯光来照明,不幸的是,他们只有一盏灯.另外,独木桥上最多能承受两个人同时经过,否则将会坍塌.每个人单独过独木桥都需要一定的时间,不同的人要的时间可能不同.两个人一起过独木桥时,由于只有一盏灯,所以需要的时间是较慢的那个人单独过桥所花费的时间.现在输入N(2<=N<1000)和这N个人单独过桥需要的时间,请计算总共最少需要多少时间,他们才能全部到达河左岸.例如,有3个人甲、乙、丙,他们单独过桥的时间分别为1、2、4,则总共最少需要的时间为7.具体方法是:甲、乙一起过桥到河的左岸,甲单独回到河的右岸将灯带回,然后甲、丙在一起过桥到河的左岸,总时间为2+1+4=7.#include<iostream>#include<cstring>using namespace std;const int size=100;const int infinity = 10000;const bool left=1;const bool right =0;const bool left_to_right=1;const bool right_to_left=0;int n,hour[size];bool pos[size];int max(int a,int b){return a>b?a:b;}int go(bool stage){int i,j,num,tmp,ans;if(stage==right_to_left){num=0;ans=0;for(i=1;i<=n;i++)if(pos[i]==right){num++;if( hour[i]>ans)ans=hour[i];}if( ① )return ans;ans=infinity;for(i=1;i<=n-1;i++)if(pos[i]==right)for(j=i+1;j<=n;j++)if(pos[j]==right){pos[i]=left;pos[j]=left;tmp=max(hour[i],hour[j])+ ②; if(tmp<ans)ans=tmp;pos[i]=right;pos[j]=right;}return ans;}if(stage==left_to_right){ans=infinity;for(i=1;i<=n;i++)if( ③ ){pos[i]=right;tmp= ④ ;if(tmp<ans)ans=tmp;⑤;}return ans;}return 0;}int main(){int i;cin>>n;for(i=1;i<=n;i++){cin>>hour[i];pos[i]=right;}cout<<go[right_to_left)<<endl;return 0;}【算法】利用深搜,左右交替寻找最优解(maybe是动态规划)【代码】1、num<=2;2、go[1];3、pos[j]==1;4、hour[i]+go[0];5、pos[i]=1;【年份】2010年三、【题目】(子矩阵)给输入一个n1*m1的矩阵a,和n2*m2的矩阵b,问a中是否存在子矩阵和b相等。
noip初赛知识点总结一、基础知识1.1 编程语言NOIP初赛主要使用C/C++和Pascal两种编程语言进行比赛。
参赛者需要熟练掌握这两种语言的基本语法和常用库函数,包括输入输出、变量声明、条件语句、循环语句、数组、字符串处理等。
1.2 数据结构参赛者需要了解各种常用的数据结构,包括数组、链表、栈、队列、堆、树、图等,以及它们的基本操作和应用场景。
此外,还需要掌握算法导论中的基本排序算法和查找算法,如插入排序、归并排序、快速排序、线性查找、二分查找等。
1.3 算法思想参赛者需要熟悉各种常见的算法思想,包括贪心算法、动态规划、分治算法、回溯算法、递归算法等,以及它们的应用场景和解题技巧。
此外,还需要了解图论中的基本算法,如最短路径算法、最小生成树算法、拓扑排序算法等。
1.4 数学知识NOIP初赛中经常涉及一些数学知识,参赛者需要了解基本的数论知识、组合数学知识、概率论知识、图论知识等,以便解决一些与数学相关的问题。
此外,还需要掌握常见的数学运算和函数求值方法。
二、经典题型2.1 模拟题模拟题一般是指模拟真实生活中的某种场景,要求参赛者根据题目描述进行逻辑推理和状态转移,最终得出正确的结果。
这类题型通常涉及数组、字符串、条件语句、循环语句等基本知识点,适合新手练手和熟悉编程语言。
2.2 数学题数学题一般是指涉及各种数学知识的问题,要求参赛者通过数学推导和运算得到最终结果。
这类题型通常涉及数论、组合数学、概率论、图论等知识点,适合对数学比较感兴趣的参赛者。
2.3 搜索题搜索题一般是指在给定的状态空间中,通过一定的搜索策略找到满足条件的解。
这类题型通常涉及深度优先搜索、广度优先搜索、状态压缩、剪枝等知识点,适合对算法思想比较感兴趣的参赛者。
2.4 动态规划题动态规划题一般是指通过维护一张状态转移表或者状态转移方程,找到最优解。
这类题型通常涉及最长上升子序列、最大子段和、背包问题、最优二叉搜索树等知识点,适合对算法思想比较感兴趣的参赛者。
NOIP常用算法代码(Pascal)1.数论算法//1.1 最大公约数gcd functiongcd(a,b:longint):longint; beginif b=0 then exit(a) else exit(gcd(b,a mod b));end;//1.2 最小公倍数lcm functionlcm(a,b:longint):longint; beginlcm:=a div gcd(a,b)*b; end;//notice:要先除后乘,避免溢出//1.3.1 快速幂运算pow functionpow(x,n:longint):longint; beginif n=0 then exit(1);pow:=pow(x,n div 2);pow:=pow*pow;if (n and 1)=1 then pow:=pow*x;end;//1.3.2 快速幂运算pow (mod P)functionpow(x,n,P:longint):longint;ov erload;beginif n=0 then exit(1);pow:=pow(x,n div 2,P);pow:=int64(pow)*pow mod P; //注意小心溢出if (n and 1)=1 then pow:=int64(pow)*x mod P;end;//1.3.3 快速幂非递归functionpow0(x,n:longint):longint; var result:longint;beginresult:=1;while n<>0 dobeginif (n and 1)=1 then result:=result*x;x:=x*x; n:=n shr 1;end;exit(result);end;//1.4 朴素判断素数is_prime functionis_prime(x:longint):boolean; var i:longint;beginfor i:=2 to round(sqrt(x)) doif x mod i=0 thenexit(false);if x<2 then exit(false) else exit(true);end;//1.5 素数的筛法procedureget_prime(n:longint);//计算1-n之间的素数,存储在list数组中var i,j:longint;beginfor i:=2 to n do if not f[i] thenbegininc(top);list[top]:=i;j:=i+i;while j<=n dobeginf[j]:=true;inc(j,i);end;end;end;//1.5.2 素数的筛选加强版program prime2; const maxn=40000;maxm=100001;var l,r,i,j:longint;f:array[0..maxn] of boolean;q:array[0..maxm] of boolean;beginfillchar(f,sizeof(f),false); for i:=2 toround(sqrt(maxn)) do if not f[i] thenbegin j:=i+i;while j<=maxn do beginf[j]:=true;inc(j,i);end;end;readln(l,r);for i:=2 toround(sqrt(maxn)) do if not f[i] thenbeginj:=l-l mod i;if j<l then inc(j,i); if j=i then inc(j,i); while j<=r dobeginq[j-l]:=true;inc(j,i);end;end;if l=1 then q[0]:=true;for i:=l to r doif not q[i-l] then writeln(i); end.//1.6 模运算下的除法(mod P) // a/b mod P = a*b^(P-2) mod Pfunctiondivide(a,b,P:longint):longint; begindivide:=a*pow(b,P-2,P) mod P;end;//1.7求组合数program zuheshu;var n,m,i,j,k:longint;c:array[0..100,0..100] of longint;functionc1(n,m:longint):int64; var i,j:longint;ans:int64;beginans:=1;for i:=n-m+1 to n dobeginj:=i;ans:=int64(ans)*j div (j-n+m);end;exit(ans);end;functiongcd(a,b:longint):longint;beginif b=0 then exit(a) else exit(gcd(b,a mod b));end;functionc2(n,m:longint):longint;var i,j,s,t,ans,x:longint;a,b:array[1..100] oflongint;begins:=0;for i:=m+1 to n dobegin inc(s); a[s]:=i; end; for i:=1 to n-m do b[i]:=i; for i:=1 to n-m dobeginif a[i]=1 then continue; for j:=1 to n-m dobeginif b[j]=1 then continue;t:=gcd(a[i],b[j]);a[i]:=a[i] div t;b[j]:=b[j] div t;end;end;ans:=1;for i:=1 to n-m doans:=ans*a[i];exit(ans); end;beginreadln(n,m);c[0,0]:=1;for i:=0 to n do c[i,0]:=1; for i:=1 to n dofor j:=1 to n doc[i,j]:=c[i-1,j]+c[i-1,j-1]; writeln(c[n,m]);writeln(c1(n,m));writeln(c2(n,m));end.2.高精度运算constNUMLEN = 100; //高精度数长度DLEN = 4; //压位长度MM = 10000; //压位进制// ps.若有乘法建议设置DLEN=3,MM=1000typebignum =array[0..NUMLEN]of longint;vara,b,c:bignum;s:ansistring;procedure print(vara:bignum);var i:longint;beginwrite(a[a[0]]);for i:=a[0]-1 downto 1 dobeginif a[i]<1000 then write(0); //若MM=1000,注释掉本行if a[i]<100 thenwrite(0);if a[i]<10 then write(0);write(a[i]);end;writeln;end;operator + (vara,b:bignum)c:bignum; var i:longint;beginfillchar(c,sizeof(c),0);if a[0]>b[0] thenc[0]:=a[0] else c[0]:=b[0];for i:=1 to c[0] dobegininc(c[i],a[i]+b[i]);if c[i]>=MM thenbegindec(c[i],MM);inc(c[i+1]);end;end;if c[c[0]+1]<>0 then inc(c[0]);end;operator - (vara,b:bignum)c:bignum; var i:longint;beginfillchar(c,sizeof(c),0);c[0]:=a[0];for i:=1 to c[0] dobegininc(c[i],a[i]-b[i]);if c[i]<0 thenbegininc(c[i],MM);dec(c[i+1]);end;end;while (c[0]>1) and (c[c[0]]=0) do dec(c[0]); end;operator * (vara,b:bignum)c:bignum;var i,j:longint;beginfillchar(c,sizeof(c),0);c[0]:=a[0]+b[0]-1;for i:=1 to a[0] dofor j:=1 to b[0] doinc(c[i+j-1],a[i]*b[j]);for i:=1 to c[0] dobegininc(c[i+1],c[i] div MM);c[i]:=c[i] mod MM;end;if c[c[0]+1]<>0 theninc(c[0]);end;procedure convert(vara:bignum;b:longint);overload; beginfillchar(a,sizeof(a),0);repeatinc(a[0]);a[a[0]]:=b mod MM;b:=b div MM;until b=0;end;procedure convert(vara:bignum;s:ansistring);overlo ad;var i,t,n:longint;beginn:=length(s);a[0]:=(n-1)div DLEN+1;for i:=1 to n dobegint:=(n-i)div DLEN+1;a[t]:=a[t]*10+ord(s[i])-ord('0');end;end;beginreadln(s); convert(a,s);readln(s); convert(b,s);c:=a*b;print(c);end.3.排序//3.1 排序算法 quicksort procedure sort(l,r:longint); var i,j,tmp,mid:longint; begini:=l; j:=r;mid:=a[(l+r+r)div 3];while i<=j dobeginwhile a[i]<mid doinc(i);while a[j]>mid do dec(j);if i<=j thenbegintmp:=a[i]; a[i]:=a[j];a[j]:=tmp;inc(i); dec(j);end;end;if i<r then sort(i,r);if l<j then sort(l,j);end;//3.2 双关键字快排proceduremulti_sort(l,r:longint);var i,j,tmp,mid0,mid1:longint; begini:=l; j:=r;mid0:=a[(l+r+r)div 3];mid1:=b[(l+r+r)div 3];while i<=j dobeginwhile (a[i]<mid0) or ((a[i]=mid0)and(a[i]<mid1)) do inc(i);while (a[j]>mid0) or ((a[j]=mid0)and(a[j]>mid1)) do dec(j);if i<=j thenbegintmp:=a[i]; a[i]:=a[j]; a[j]:=tmp;tmp:=b[i]; b[i]:=b[j]; b[j]:=tmp;inc(i); dec(j);end;end;if i<r then multi_sort(i,r);if l<j then multi_sort(l,j); end;//3.3插入排序procedure insert_sort;var i,j:longint;beginfor i:=2 to n dobegina[0]:=a[i];j:=i-1;while a[0]<a[j] dobegina[j+1]:=a[j];dec(j);end;a[j+1]:=a[0];end;end;//3.4堆排procedure heap_sort;procedure sift(i,m:longint); var k:integer;begina[0]:=a[i];k:=2*i;while k<=m dobeginif(k<m)and(a[k]<a[k+1]) then inc(k);if a[0]<a[k] thenbegin a[i]:=a[k];i:=k;k:=i+i;endelse break;end;a[i]:=a[0];end;var i,j:longint;beginfor i:=n div 2 downto 1 do sift(i,n);for i:=n downto 2 dobegina[0]:=a[i]; a[i]:=a[1];a[1]:=a[0];sift(1,i-1);end;end;//3.5归并排序procedure merge_sort(var r,r1:arr; s,t:longint);procedure merge(r:arr;l,m,n:longint; var r2:arr);var i,j,k,p:longint;begini:=l; j:=m+1; k:=l-1;while (i<=m)and(j<=n) dobegininc(k);if r[i]<=r[j] then begin r2[k]:=r[i]; inc(i); endelse beginr2[k]:=r[j]; inc(j); endend;if i<=m thenbeginfor p:=i to m dobegin inc(k);r2[k]:=r[p]; end;end;if j<=n thenbegin for p:=j to n dobegin inc(k);r2[k]:=r[p]; end;end;end;var k:longint;c:arr;beginif s=t then r1[s]:=r[s]else begink:=(s+t) shr 1;merge_sort(r,c,s,k) ;merge_sort(r,c,k+ 1,t);merge(c,s,k,t,r1); end;end;4.树状数组//4.1.基本操作functionsum(i:longint):longint;var result:longint;beginresult:=0;while i>0 dobegininc(result,a[i]);dec(i,i and -i);end;exit(result);end;procedure add(i,d:longint); beginwhile i<=MAXM dobegininc(a[i],d);inc(i,i and -i);end;end;//4.2应用举例A.求逆序对constMAXN=200000;vara,d,p,h:array[0..MAXN]of longint;n,t,i,j:longint;ans:int64;procedure sort(constl,r:longint);vari,j,tmp,mid:longint;begini:=l; j:=r;mid:=d[(l+r+r)div 3];repeatwhile d[i]>mid doinc(i);while d[j]<mid do dec(j);if i<=j then begintmp:=d[i]; d[i]:=d[j]; d[j]:=tmp;tmp:=p[i]; p[i]:=p[j]; p[j]:=tmp;inc(i); dec(j);end;until i>j;if i<r then sort(i,r);if l<j then sort(l,j);end;functionsum(i:longint):longint;beginsum:=0;while i<>0 do begininc(sum,a[i]);dec(i,i and -i);end;end;procedure add(i,d:longint); beginwhile i<=n do begininc(a[i],d);inc(i,i and -i);end;end;beginassign(input,'B.in'); reset(input);assign(output,'B.out'); rewrite(output);readln(n);for i:=1 to n do beginreadln(d[i]);p[i]:=i;end;sort(1,n);t:=1; h[p[1]]:=t;for i:=2 to n do beginif d[i]<>d[i-1] then inc(t);h[p[i]]:=t;end;ans:=0;for i:=1 to n do begint:=sum(h[i]-1);ans:=ans+t;add(h[i],1);end;writeln(ans);close(input);close(output);end.B.求最长上升子序列vardata,a,f:array[1..MAXN]of longint;functiongetmax(i:longint):longint; begingetmax:=0;while i<>0 dobeginif a[i]>getmax then getmax:=a[i];dec(i,i and -i);end;end;procedurechange(i,d:longint); beginwhile i<=n dobeginif d>a[i] then a[i]:=d;inc(i,i and -i);end;end;beginfor i:=1 to n dobeginf[i]:=getmax(data[i])+1;change(data[i],f[i]);end;end.5.搜索//5.1深搜(N皇后的对称优化及位运算)program Nhuanghou(对称优化);const maxn=100;var n,n1,n2:integer;x:array[1..maxn] of integer; a:array[1..maxn] of boolean;b:array[2..maxn*2] of boolean;c:array[1-maxn..maxn-1] of boolean;s,t1,t2:boolean; procedure out(var s:boolean); var j:integer; beginif odd(n) thenbeginif not t1 thenif x[1]>n div 2 thenbeginn1:=count;t1:=true;end;if t1 and not t2 thenif x[1]>(n+1) div 2 then beginn2:=conut;t2:=true;end;if t1 and t2 thenbegincount:=n1+n2;s:=true;exit;end;endelse beginif x[1]>n div 2 then begincount:=count*2; s:=true;exit;end;end;inc(count);for j:=1 to n do write(x[j],' ');end;procedure place(j:integer); var i:integer;beginfor i:=1 to n doif not (a[i] or b[i+j] or c[i-j]) thenbeginx[j]:=i;a[i]:=true; b[i+j]:=true; c[i-j]:=true;if j<n then place(j+1) else beginout(s);if s then begin writeln (count);halt; end;end;a[i]:=false;b[i+j]:=false; c[i-j]:=false;end;end;beginreadln(n);place(1);end.program checker;(位运算)varlimite,n,t:longint;r:array[1..13]of integer;procedure print;var i:integer;beginfor i:=1 to n-1 dowrite(r[i],' ');writeln(r[n]);end;proceduretry(row,ld,rd,lv:longint);varp,pos:longint;beginif row<limite then begin pos:=limite and not(row or ld or rd);while pos<>0 dobeginp:=pos and -pos;pos:=pos-p;if t<=3 thenr[lv]:=round(ln(p)/ln(2))+1; try(row or p,(ld+p)shl 1,(rd+p) shr 1,lv+1); endendelse begininc(t);if t<=3 then print;end;end;beginreadln(n);limite:=(1 shl n)-1;try(0,0,0,1);writeln(t);end.//5.2双向广搜(字符串的变换)const maxn=100;typenode=records:string;depth:integer;//father:integer;end;varq:array[0..1,1..maxn]of node; head,tail:array[0..1]of integer;s1,s2:string;i,n:integer;procedure init;beginassign(input,'a1.in'); reset(input);readln(s1);readln(s2);n:=length(s1);close(input);end;procedure print(m:integer); beginif m=-1 then writeln('No answer')else writeln(m);halt; end;function find(t:integer; ss:string):boolean;var i:integer;beginfor i:=1 to tail[t] doif ss=q[t,i].s thenexit(true);exit(false);end;procedurecheckmeet(t:integer);var i:integer;beginfor i:=1 to tail[1-t] doif q[t,tail[t]].s=q[1-t,i].s thenprint(q[t,tail[t]].depth+ q[1-t,i].depth);end;procedureexpand(t:integer);var i:integer; s,ss:string; step:integer;begininc(head[t]);s:=q[t,head[t]].s;step:=q[t,head[t]].depth+1; for i:=1 to n-1 doif s[i]<>s[i+1] thenbeginss:=s; ss[i]:=s[i+1]; ss[i+1]:=s[i];if not find(t,ss) then begininc(tail[t]);q[t,tail[t]].s:=ss;q[t,tail[t]].depth:=st ep;checkmeet(t);end;end;end;procedure doubfs; beginhead[0]:=0; tail[0]:=1; q[0,1].s:=s1;head[1]:=0; tail[1]:=1; q[1,1].s:=s2;while (head[0]<tail[0]) and (head[1]<tail[1]) do beginif tail[0]<tail[1]then expand(0)else expand(1);end;print(-1);end;Begininit;doubfs;End.6.图论//6.1SSSP(单源最短路径) 6.1.1Dijkstra(输出路径)Const maxn=100;vara:array[1..maxn,1..maxn] of integer;d:array[1..maxn] of integer; path:array[1..maxn] of integer;f:array[1..maxn] of boolean; n,ks,mb:integer;procedure init;var i,j,k:integer;beginassign(input,fin);reset(input);readln(n);for i:=1 to n do for j:=1 to n do a[i,j]:=maxint;for i:=1 to n do a[i,i]:=0; readln(ks,mb);while not seekeof dobeginreadln(i,j,k); a[i,j]:=k; a[j,i]:=k;end;close(input);end;procedure dijkstra;var i,j,k,min:integer;beginfor i:=1 to n dobegin d[i]:=a[ks,i];f[i]:=false; path[i]:=ks; end; f[ks]:=true;for i:=2 to n dobeginmin:=maxint;for j:=1 to n doif (not f[j]) and (d[j]<min) thenbegin min:=d[j]; k:=j; end;f[k]:=true;for j:=1 to n doif (not f[j]) and (d[k]+a[k,j]<d[j]) thenbegind[j]:=d[k]+a[k,j]; path[j]:=k; //j的前驱k;end;end;end;procedure print;procedure dfs(i:integer); beginif path[i]<>ks thenbegindfs(path[i]);write(path[i],' ');end;end;beginwriteln(d[mb]);write(ks,' '); dfs(mb); writeln(mb);end;begininit;dijkstra;print;end.6.1.2 SPFAconstMAXN = 10000;MAXM = 100000;varv,w,next:array[1..MAXM]of longint;head:array[1..MAXN]of longint;n,m:longint;d,q:array[1..MAXN]of longint;vis:array[1..MAXN]of boolean;procedure init;vari,x:longint;beginreadln(n,m);for i:=1 to m dobeginreadln(x,v[i],w[i]);next[i]:=head[x];head[x]:=i;end;end;procedure spfa(x:longint); var i,l,r,y:longint;beginfillchar(d,sizeof(d),$3f);fillchar(vis,sizeof(vis),0);l:=0; r:=1; vis[x]:=true;d[x]:=0; q[1]:=x;while l<>r dobegininc(l); if l>MAXN then l:=1;x:=q[l];i:=head[x];while i<>0 dobeginy:=v[i];if d[x]+w[i]<d[y] thenbegind[y]:=d[x]+w[i];if not vis[y] thenbeginvis[y]:=true;inc(r); ifr>MAXN then r:=1;q[r]:=y;end;end;i:=next[i];end;vis[x]:=false;end;end;procedure main;var i:longint;beginspfa(1);for i:=1 to n do write(d[i],' ');end;begininit;main;end.//6.2 最小生成树6.2.1Primprogram prim;const maxn=1000;var a:array[1..maxn,1..maxn] of longint;d:array[1..maxn] of longint; pre:array[1..maxn] of integer; f:array[1..maxn] of boolean;tree:array[1..maxn-1,1..2] of integer;n,ans:longint; procedure init;var i,j,k:longint;beginreadln(n);fillchar(a,sizeof(a),$3f);for i:=1 to n do a[i,i]:=0; while not seekeof dobeginreadln(i,j,k);a[i,j]:=k; a[j,i]:=k;end;end;procedure prim;var i,j,k,min:longint;beginfillchar(tree,sizeof(tree),0); for i:=1 to n dobegind[i]:=a[1,i]; pre[i]:=1; f[i]:=false;end;f[1]:=true; ans:=0;for i:=1 to n-1 dobeginmin:=maxlongint; k:=0; for j:=1 to n doif (notf[i])and(d[j]<min) thenbeginmin:=d[j]; k:=j;end;tree[i,1]:=k;tree[i,2]:=pre[k];inc(ans,d[k]);f[k]:=true;for j:=1 to n doif (notf[j])and(a[k,j]<d[j]) thenbegin d[j]:=a[k,j]; pre[j]:=k; end;end;end;procedure print;var i,j:longint;beginwriteln(ans);for i:=1 to n-1 do writeln(tree[i,1],' ',tree[i,2]); end;begininit;prim;print;end.6.2.2 Kruskal(MST)//time : O(mlongm) constMAXN = 10000;MAXM = 100000;varu,v,w:array[1..MAXM]of longint;fa:array[1..MAXN]of longint;n,m,x,y,i,left:longint;functiongetfa(i:longint):longint; beginif fa[i]=0 then exit(i);fa[i]:=getfa(fa[i]);exit(fa[i]);end;procedure swap(vara,b:longint);var t:longint;begint:=a; a:=b; b:=t; end;procedure sort(l,r:longint);var i,j,mid:longint; begini:=l; j:=r;mid:=w[(l+r+r)div 3];while i<=j dobeginwhile w[i]<mid do inc(i);while w[j]>mid do dec(j);if i<=j thenbeginswap(u[i],u[j]);swap(v[i],v[j]);swap(w[i],w[j]);inc(i); dec(j);end;end;if i<r then sort(i,r);if l<j then sort(l,j); end;beginreadln(n,m);for i:=1 to m do readln(u[i],v[i],w[i]);sort(1,m);left:=n-1;for i:=1 to m dobeginx:=getfa(u[i]);y:=getfa(v[i]);if x=y then continue;fa[x]:=y;writeln(u[i],' ',v[i],' ',w[i]);dec(left);if left=0 then break;end;end.//6.3 拓扑排序//algorithm : toposort //time : O(m)constMAXN = 10000;MAXM = 100000;varv,next:array[1..MAXM]of longint;head:array[1..MAXN]of longint;top,n,m:longint;vis:array[1..MAXN]of boolean;vex:array[1..MAXN]of longint;procedure init;vari,x:longint;beginreadln(n,m);for i:=1 to m dobeginreadln(x,v[i]);next[i]:=head[x];head[x]:=i;end;end;procedure dfs(x:longint); var i:longint;beginvis[x]:=true;i:=head[x];while i<>0 dobeginif not vis[v[i]] then dfs(v[i]);i:=next[i];end;vex[top]:=x; dec(top); end;procedure main;var i:longint;begintop:=n;for i:=1 to n do if not vis[i] then dfs(i);for i:=1 to n dowrite(vex[i],' ');end;begininit;main;end.//6.4 哈密顿(回)路{哈密顿路:标准的算法} programhmdlu(input,output); const maxn=100;vara:array[1..maxn,1..maxn]ofinteger;visited:array[1..maxn]of boolean;b:array[1..maxn] of integer; n,m:integer;procedure init;var i,j:integer;beginassign(input,'a.in');reset(in put);readln(n);for i:=1 to n dofor j:=1 to n doread(a[i,j]);close(input);end;procedure print(k:integer); var i:integer;beginif k=0 then writeln('no round') elsebegin for i:=1 to n-1 do write(b[i],' ');writeln(b[n]);end;halt;end;procedure dfs(i,k:integer); var j:integer;beginif k=n then print(1);for j:=1 to n doif (a[j,i]=1) and (visited[j]=false) thenbeginvisited[j]:=true;b[k+1]:=j;dfs(j,k+1);visited[j]:=false;end;end;procedure main;var i:integer;beginfor i:= 1 to n dobeginfillchar(visited,sizeof(vi sited),false);b[1]:=i;visited[i]:=true;dfs(i,1);end;print(0);end;begininit;main;end.{哈密顿回路路:标准的算法} programhmdlu(input,output);const maxn=100;varvisited:array[1..maxn]of integer;b:array[1..maxn] of 1..maxn;n,m,i:integer;found:integer;functioncheck(i:integer):boolean;var j:integer;beginfor j:=2 to i-1 doif i mod j=0 thenbegincheck:=false;exit;end;check:=true;end;procedure print(k:integer); var i:integer;beginif k=1 thenbeginfor i:=1 to n-1 do write(b[i],' ');writeln(b[n]);endelse writeln('no road'); halt;end;procedure dfs(i,m:integer); var j,k:integer;beginif(n=m)and(check(b[1]+b[m])) then print(1);for j:=1 to n doif (check(i+j)) and (visited[j]=0) thenbeginvisited[j]:=1;b[m+1]:=j;dfs(j,m+1);visited[j]:=0;end; end;procedure main;beginfillchar(visited,sizeof(visit ed),0);b[1]:=1;visited[1]:=1;dfs(1,1);print(-1);end;beginreadln(n);main;end.//6.5 欧拉(回)路program necklace;var a:array[1..100,1..100] of longint;ans:array[1..1001] of longint;d:array[1..100] of integer;x,y,i,j,k,s,t,l,top,n,m:longint; f:boolean;procedure dfs(cur:longint); var i:longint;beginfor i:=1 to 100 doif a[cur,i]>0 thenbegindec(a[cur,i]);dec(a[i,cur]);dfs(i);end;inc(top); ans[top]:=cur;end;beginreadln(t);for l:=1 to t dobeginfillchar(d,sizeof(d),0);fillchar(a,sizeof(a),0);readln(n);for i:=1 to n do beginreadln(x,y);inc(a[x,y]); inc(a[y,x]); inc(d[x]); inc(d[y]);end;top:=0; f:=false;for i:=1 to 100 doif d[i]>0 thenif d[i] and 1=1 then begin f:=true; break; end;if not f thenbeginfor i:=1 to 100 doif d[i]>0 thenbegindfs(i); break;end;if (top=n+1) then beginfor i:=1 to top-1 do writeln(ans[i],' ',ans[i+1]);f:=false;end;end;if f then writeln('some beads may be lost');if l<>t then writeln;end;end.//6.6 Floyed求最小环constlimit=65535;vardis,g,path:array[1..100,1..10 0]of word;function print(i,j:word):string; var s:string;beginif path[i,j]=0 thenbeginstr(i,s);s:=s+' '; exit(s);end;s:='';s:=s+print(i,path[i,j]);s:=s+print(path[i,j],j);exit(s);end;procedure work;varn:integer;i,j,k,u,v,w,m,min:word;s:string;beginread(n);if n=-1 then halt;fillchar(dis,sizeof(dis),255) ;fillchar(path,sizeof(path), 0);readln(m);for i:=1 to m do beginreadln(u,v,w);dis[u,v]:=w;dis[v,u]:=w;end;s:='';min:=limit;u:=0;v:=0;w:=0 ;g:=dis;for k:=1 to n dobeginfor i:=1 to k-1 dofor j:=i+1 to k-1 do if g[i,j]<limit then if g[i,j]+dis[i,k]+dis[k,j]<min thenbeginmin:=g[i,j]+dis[i,k] +dis[k,j];u:=i;v:=j;w:=k;s:=print(u,v);end;for i:=1 to n dofor j:=1 to n doif g[i,k]+g[k,j]<g[i,j] thenbeging[i,j]:=g[i,k]+g[k,j];path[i,j]:=k;end;end;if s='' thenwriteln('No solution.') else beginwrite(s);writeln(v,' ',w);end;end;beginwhile true do work end.7.单调队列vara,q:array[1..MAXN]of longint;n,k,i,j,head,tail:longint; beginreadln(n,k);for i:=1 to n doreadln(a[i]);head:=1; tail:=0;for i:=1 to n dobeginif (head<=tail) and (q[head]=i-k) then inc(head);while (head<=tail) and (a[i]<a[q[tail]]) do dec(tail);inc(tail); q[tail]:=i;writeln(a[q[head]]);end;end.8.Hash//8.1 字符串constP:int64 = (1 shl 50)-1;vars1,s2:ansistring;i,l1,l2:longint;t1,t2,m:int64;beginl1:=length(s1);l2:=length(s2);for i:=1 to l2 dot2:=(t2*131+ord(s2[i]))and P;for i:=1 to l2 dot1:=(t1*131+ord(s1[i]))and P;m:=1;for i:=1 to l2-1 dom:=(m*131)and P;s1:=s1+' ';for i:=l2+1 to l1+1 dobeginif t1=t2 thenwriteln('pos : ',i-l2);t1:=(((t1-(ord(s1[i-l2])*m and P)+P+1)*131)+ord(s1[i]))and P;end;end.//8.2康托展开{1,2,3,4,...,n}表示1,2,3,...,n 的排列如 {1,2,3} 按从小到大排列一共6个 123 132 213 231 312 321代表的数字 1 2 3 4 5 6 也就是把10进制数与一个排列对应起来。