《基础代码汇总整理forNOIP2009》WQ@YCOIwangqian1992511@gmail.com十进制转换K进制functiondectok(x,k:longint):string;constalph='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';varst:string;beginst:='';whilex<>0dobeginst:=alph[xmodk+1]+st;x:=xdivk;end;exit(st);end;K进制转换十进制functionktodec(st:string;k:longint):longint;constalph='012456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';vari,j,ans:longint;beginans:=0;j:=1;fori:=length(st)downto1dobegininc(ans,j*(pos(st[i],alph)-1));j:=j*k;end;exit(ans);end;欧几里得算法functiongcd(a,b:longint):longint;beginifb=0thenexit(a)elseexit(gcd(b,amodb));end;求最小公倍数functionlcm(a,b:longint):longint;beginexit(adivgcd(a,b)*b);end;判断质数functionjudgeprime(x:longint);vari:longint;beginifx=1thenexit(false);fori:=2totrunc(sqrt(x))doifxmodi=0thenexit(false);exit(true);end;生成质数表proceduremakeprime;vari,j:longint;beginfillchar(f,sizeof(f),0);f[1]:=true;fori:=2tondoif(notf[i])and(i<10000)thenbeginj:=i*i;whilej<=ndobeginf[j]:=true;inc(j,i);end;end;end;简单高精度运算系列procedurechange(st:string;varx:arrayoflongint);beginx[0]:=0;whilelength(st)>=4dobegininc(x[0]);val(copy(st,length(st)-3,4),x[x[0]]);delete(st,length(st)-3,4);end;inc(x[0]);val(st,x[x[0]]);end;functioncompare(a,b:arrayoflongint):boolean;vari:longint;beginifa[0]>b[0]thenexit(true);ifa[0]<b[0]thenexit(false);fori:=a[0]downto1doifa[i]>b[i]thenexit(true)elseifa[i]<b[i]thenexit(false);exit(true);end;procedurehighplus(a,b:arrayoflongint;varc:arrayoflongint);vari:longint;beginfillchar(c,sizeof(c),0);ifa[0]>b[0]thenc[0]:=a[0]elsec[0]:=b[0];fori:=1toc[0]doinc(c[i],a[i]+b[i]);fori:=1toc[0]doifc[i]>=10000thenbegindec(c[i],10000);inc(c[i+1]);end;whilec[c[0]+1]>0doinc(c[0]);end;procedurehighminus(a,b:arrayoflongint;varc:arrayoflongint);vari:longint;beginfillchar(c,sizeof(c),0);c[0]:=a[0];fori:=1toc[0]doinc(c[i],a[i]-b[i]);fori:=1toc[0]doifc[i]<0thenbegininc(c[i],10000);dec(c[i+1]);end;while(c[0]<>1)and(c[c[0]]=0)dodec(c[0]);end;procedurehighmulti(a,b:arrayoflongint;varc:arrayoflongint);vari,j:longint;beginfillchar(c,sizeof(c),0);c[0]:=a[0]+b[0]-1;fori:=1toa[0]doforj:=1tob[0]doinc(c[i+j-1],a[i]*b[j]);fori:=1toc[0]doifc[i]>10000thenbegininc(c[i+1],c[i]div10000);c[i]:=c[i]mod10000;end;whilec[c[0]+1]>0doinc(c[0]);end;procedurehighout(x:arrayoflongint);vari:longint;beginwrite(x[x[0]]);fori:=x[0]-1downto1dobeginifx[i]<1000thenwrite(0);ifx[i]<100thenwrite(0);ifx[i]<10thenwrite(0);write(x[i]);end;writeln;end;表达式求值constnum='0123456789';sym='+-*/()@';com:array[1..7,1..7]oflongint=((1,1,-1,-1,-1,1,1),(1,1,-1,-1,-1,1,1),(1,1,1,1,-1,1,1),(1,1,1,1,-1,1,1),(-1,-1,-1,-1,-1,0,2),(1,1,1,1,2,1,1),(-1,-1,-1,-1,-1,2,0));functioncalc(suf:string):double;varstack:array[1..100]ofdouble;i,top:longint;x:double;ch:char;begini:=1;ch:=suf[1];top:=0;whilech<>'@'dobegincasechof'+':beginx:=stack[top-1]+stack[top];dec(top,2);end;'-':beginx:=stack[top-1]-stack[top];dec(top,2);end;'*':beginx:=stack[top-1]*stack[top];dec(top,2);end;'/':beginx:=stack[top-1]/stack[top];dec(top,2);end;'0'..'9':beginx:=0;whilech<>''dobeginx:=x*10+pos(ch,num)-1;inc(i);ch:=suf[i];end;endend;inc(top);stack[top]:=x;inc(i);ch:=suf[i];end;exit(stack[top]);end;procedureturn(varmid,suf:string);varstack:array[1..100]oflongint;i,top,w:longint;ch:char;beginmid:=mid+'@';suf:='';stack[1]:=7;i:=1;top:=1;ch:=mid[1];whilech<>'@'dobeginifpos(ch,num)<>0thenbeginwhilepos(ch,num)<>0do...