pascal的几道题

pascal的几道题,第1张

1、求100以内的素数(质数,用筛选法)

program exe6_1;

var a:array[2100] of boolean; i,k: integer;

begin

for i:=2 to 100 do a[i]:=true;

for i:=2 to 49 do

if a[i] then

for k:=i+1 to 100 do

if i mod k=0 then a[i]:=false ;

writeln(‘zhi shu:’);

for i:=2 to 100 do

if a[i] then write(i:4);

end

2

const n=5;

var a:array[1n,1n] of integer;

b:array[1100,13] of integer;

i,j,k:integer;

begin

writeln('read array a:');

for i:=1 to n do

for j:=1 to n do

read (a[i,j]);

k:=0;

for i:=1 to n do

for j:=1 to n do

if a[i,j]<>0 then

begin

k:=k+1;

b[k,1]:=i;

b[k,2]:=j;

b[k,3]:=a[i,j] ;

end;

writeln('Xi Shu:');

for i:=1 to k do

begin

for j:=1 to 3 do

write(b[i,j]:3);

writeln;

end;

end

3

type

atype=array[150] of integer;

var

i, count, y: integer;

a: atype;

procedure p(var x :atype; y,n: integer; var m: integer); { 用到了n,只好写这了 }

var i, k :integer;

begin

if m>=n then writeln('out range')

else begin

k:=1;

while x[k+1]<y do k:=k+1;

for i:=m downto k do

x[i+1]:=x[i];

x[m]:=y;

m:=m+1;

end;

end;

begin

readln(count);

for i:=1 to count do

a[i]:=3i;

for i:=1 to count do

write(a[i]:4);

write('y='); readln(y);

p(a,y,50,count);

for i:=1 to count do

write(a[i]:4);

writeln;

end

先使用快速排序对漂亮值由大到小排序,然后在s值中寻找最长上升子序列。对于本题来说,是最长不下降子序列,而且要注意漂亮值相等时要特殊处理。

var

b,s,v:array[0100000] of longint;

n,i,j,k,max,ans:longint;

procedure qsort(l,r:longint);

var i,j,t,x:longint;

begin

i:=l; j:=r; x:=(i+j) div 2;

repeat

while b[i]>b[x] do inc(i);

while b[j]<b[x] do dec(j);

if j>=i then

begin

t:=b[i];

b[i]:=b[j];

b[j]:=t;

t:=s[i];

s[i]:=s[j];

s[j]:=t;

inc(i);

dec(j);

end;

until i>j;

if l<j then qsort(l,j);

if i<r then qsort(i,r);

end;

procedure sort(l,r:longint);

var i,j,t:longint;

begin

for i:=r-1 downto l do

for j:=l to i do

if s[j]>s[j+1] then

begin

t:=s[j];

s[j]:=s[j+1];

s[j+1]:=t;

end;

end;

begin

readln(n);

for i:=1 to n do readln(s[i],b[i]);

qsort(1,n);

i:=0;

repeat

inc(i);

if b[i+1]=b[i] then

begin

k:=i;

while (b[k]=b[k+1])and(k<=n) do inc(k);

sort(i,k);

i:=k;

end;

until i>n;

ans:=-maxlongint;

for i:=1 to n do

begin

max:=-maxlongint;

for j:=i downto 0 do if (s[i]>=s[j]) and ((v[j]+1)>max) then max:=v[j]+1;

v[i]:=max;

if max>ans then ans:=max;

end;

writeln(ans);

end

以上就是关于pascal的几道题全部的内容,包括:pascal的几道题、请教一道pascal程序题、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9772768.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存