求pascal阅读程序题
1.请将以下程序段表示的计算公式写出来(假设X的值已给出)
E:=1; A:=1;
FOR N:=1 TO 10 DO
BEGIN
A:=A*X/N;
E:=E+A;
ENDFOR;
写出所表示的公式。
2.阅读下列程序段,写出程序段运行后变量X的值。
100 X1=3
X1:=3;
X2:=8;
FOR I:=1 TO 5 DO
BEGIN 循环结构,应用数据轮换方式,求
X:=(X1+X2)*2; 两个数和的2倍。
X1:=X2; X2:=X;
END;
WRITELN(‘X=’,X);
3.阅读下列程序段,写出程序运行后数组元素A1,A2,…,A11中的值。
A[1]:=1;
A[2]:=1; K:=1;
REPEAT
A[K+2]:=1;
FOR I:=K+1 DOWNTO 2 DO
A[I]:=A[I]+A[I-1];
K:=K+1;
UNTIL K>=10;
4. program exp1 (imput,output);
var i,s,max:integer;
a:array [1..10] of integer;
begin
for i:=1 to 10 do read(a[i]);
max:=a[1]; s:=a[1];
for i:=2 to 10 do
begin
if s<0 then s:=0;
s:=s+a[i];
if s>max then max:=s
end;
writeln(‘max=’,max)
end.
输入:-2 13 -1 4 7 8 -1 -18 24 6
输出:max=
输入:8 9 -1 24 6 5 11 15 -28 9
输出:max=
5.program exp2 (input,output);
const n=5;
var i,j,k:integer;
a:array[1..2*n, 1..2*n] of integer;
begin
k:=1;
for i:=1 to 2*n-1 do
if i<=n then
if odd(i) then for j:=i downto 1 do begin a[i-j+1,j]:=k; k:=k+1 end
else for j: =1 to i do begin a[i-j+1,j]:=k; k:=k+1; end
else if odd(i) then for j:=n downto i-n+1 do
begin a[i-j+1,j]:=k; k:=k+1; end
else for j:=i-n+1 to n do
begin a[i-j+1,j]:=k; k:=k+1; end;
for i:=1 to n do
begin
for j:=1 to n do write(a[i,j]:3);
writeln
end;
end.
6.program exp3 (input,output);
const n=10;
var s,i:integer;
function co(i1:integer):integer;
var j1,s1:integer;
begin
s1:=n; for j1:=(n-1) downto (n-i1+1) do s1:=s1*j1 div (n-j1+1);
co:=s1
end;
begin
s:=n+1; for i:=2 to n do s:=s+co(i);
writeln(‘s=’,s);
end.
7.program exp4(input,output);
const n=3;
var i,j,s,x:integer;
p:array [0..n+1] of integer;
g:array [0..100] of integer;
begin
for i := 0 to 100 do g[i]:=0;
p[0]:=0; p[n+1]:=100; for i:= 1 to n do read (p[i]); readln;
for i:= 0 to n do
for j:= i+1 to n+1 do g[abs(p[j]-p[i])]:=g[abs(p[j]-p[i])]+1;
s:=0; for i:=0 to 100 do if g[i]>0 then begin write(i,:4); s:=s+1; end;
writeln; writeln(‘s=’,s); writeln(‘input data:’); readln(x);
writeln(g[x])
end.
输入:10 20 65
input data: 10
输出:
8.program excpl;
var x,y,y1,jk,j1,g,e:integcr;
a:array [l..20] of 0..9;
begin
x:=3465; y:=264; jk:=20; for j1:=1 to 20 do a[j1]:=0;
while y<>0 do
begin
y1:=y mod 10; y:=y div 10;
while y1<>0 do
begin
g:=x;
for e:=jk downto 1 do begin g:=g+a[e]; a[e]:=g mod 10; g:=g div 10 end;
y1:=y1-1
end;
jk:=jk-1
end;
j1:=1; while a[j1]=0 do j1:=j1+1;
for jk:=j1 to 20 do write(a[jk]:4); writeln
end.
9.program excp2
var i,j:integer;
a:array [1..14] of integer;
procedure sw(i1,j1:integer);
var k1:integer;
begin
for k1:=1 to (j1-i1+1) div 2 do
begin
a[i1+k1-1]:=a[i1+k1-1]+a[j1-k1+1];
a[j1-k1+1]:=a[i1+k1-1]-a[j1-k1+1];
a[i1+k1-1]:=a[i1-k1+1]-a[j1-k1+1];
end;
end;
begin
j:=211; for i:=1 to 14 do begin a[i]:=i; j:=j-i end;
sw(1,4); sw( 5,10); sw(11,14); sw(1,14);
for i:=1 to 14 do begin if j mod i=1 then write (a[i]:3); j:=j-a[i]; end;
writeln
end.
10.program noi_002;
var i,j,l,n,k,s,t:integer;
b:array [1..10] of 0..9;
begin
readln(l,n); s:=l; k:=1; t:=l;
while s<n do begin k:=k+1; t:=t*l; s:=s+t end;
s:=s-t; n:=n-s-1; for i:=1 to 10 do b[i]:=0;
j:=11; while n>0 do begin j:=j-1; b[j]:=n mod l; n:=n div l end;
for i:=10-k+1 to 10 do write(chr(ord('a')+b[i]));
end.
输入:4 167
输出:
11.program noi_004;
var i,j,j1,j2,p,q:integer;
p1:boolean;
b,c:array [1..100] of integer;
begin
readln(q,p); j:=1; p1:=true; b[j]:=q; j1:=0;
while (q>0) and p1 do
begin
j1:=j1+1; c[j1]:=q*10 div p; q:=q*10-c[j1]*p;
if q>0 then begin
j2:=1; while (b[j2]q) and (j2<=j) do j2:=j2+1;
if b[j2]=q
then begin
p1:=false; write('0.');
for i:=1 to j2-1 do write(c[i]:1);
write('{');
for i:=j2 to j1 do write(c[i]:1);
writeln('}')
end
else begin j:=j+1; b[j]:=q end
end
end;
if q=0 then begin
write('0.');
for i:=1 to j1 do write(c[i]:1);
writeln
end;
readln
end.
输入 ① 1 8 输出
输入 ② 2 7 输出
12. program chu7_1;
function fun(x:integer):integer;
begin
if (x=0) or (x=1) then fun:=3
else fun:=x-fun(x-2)
end;
begin
writeln(fun(9));
readln;
end.
13.program chu7_2;
var i,j,f:integer;
a:array [1..8] of integer;
begin
for i:=1 to 8 do
begin
f:=i mod 2;
if f=0 then a[i]:=0
else a[i]:=1;
for j:=1 to i do
if f=0 then a[i]:=a[i]+j
else a[i]:=a[i]*j
end;
for i:=1 to 8 do write(a[i]:5);
end.
14.program chu7_3;
var p,q,s,t:integer;
begin
readln(p);
for q:=p+1 to 2*p do
begin
t:=0; s:=(p*q)mod(q-p);
if s=0 then begin t:=p+q+(p*q)div(q-p); write(t:4); end;
end;
readln
end.
输入:12
输出:
15.prgoram chu7_4;
var n,k,i:integer;
a:array [1..40] of integer;
procedure find(x:integer);
var s,i1,j1:integer;
p:boolean;
begin
i1:=0; p:=true;
while p do
begin
i1:=i1+1; s:=0;
for j1:=1 to n do if a[j1]>a[i1]then s:=s+1;
if(s=x-1)then begin writeln(a[i1]); p:=false end;
end
end;
begin
readln(n,k);
for i:=1 to n do read(a[i]);
find(k); find(n-k);
end.
输入:10 4
12 34 5 65 67 87 7 90 120 13
输出:
16.program exp1;
var i,j,k,n,,l0,l1,lk:integer;
a:array [0..20] of integer;
begin
readln(n,k);
for i:=0 to n-1 do a[i]:=i+1;
a[n]:=a[n-1]; l0:=n-1; lk:=n-1;
for i:=1 to n-1 do
begin
l1:=l0-k; if (l1<0) then l1:=l1+n;
if (l1=lk) then begin a[l0]:=a[n]; lk:=lk-1; a[n]:=a[lk]; l0:=lk end;
else begin a[l0]:=a[l1];l0:=l1; end;
end;
a[l0]:=a[n]; for i:=0 to n-1 do write(a[i]:40); writeln;
end.
输入:10 4
输出:
17.program exp2;
var n,jr,jw,jb:integer;
ch1:char;
ch:array [1..20] of char;
begin
readln(n); for i:=1 to n do read(ch[i]); jr:=1; jw:=n; jb:=n;:
while (jr<=jw) do
begin
if (ch[jw]='R')
then begin
ch1:=ch[jr]; ch[jr]:=ch[jw]; ch[jw]:=ch1; jr:=jr+13
end
else if ch[jw]='W' then jw:=jw-1
else begin
ch1:=ch[jw];ch[jw]:=ch[jb];ch[jb]:=ch1;
jw:=jw-1;jb:=jb-1;
end
end;
for i:=1 to n do write(ch[i]);
writeln;
end.
输入:10
RBRBWWRBBR
输出:
18.pmgram exp3;
var i,j,p,n,q,s:integer;
a:array [1..20] of integer;
begin
readln(p,n,q);j :=21;
while (n>0)do begin j:=j-1;a[j]:=n mod 10;n:=n div 10; end;
s:=0; for i:=j t0 20 do s:=s*p+a[i]; writeln(s); j:=21;
while (s>o)do begin j:=j-1; a[j]:=s mod q; s:=s div q; end;
for i:=j to 20 do write(a[i]);readln;
end.
输入:7 3051 8
输出:
19.program programl;
var a,x,y,okl,ok2:integer;
begin
a:=100; x:=l0; y:=20; okl:=5; ok2:=0;
if ((x>y) or ((y20) and (okl=0)) and (ok20)) then a:=1
else if ((okl0) and (ok2=、0)) then a:=-1 else a:=0;
writeln(a);
end.
20.program Program2;
var a,t:string;
i,j:integer;
begin
a:=morning; j:=l; for i:=2 to 7 do if (a[j]<a[i]) then j:=i;
j:=j-1; for i:=1 to j do write(a[i]);
end.
21.program program3;
var a,b,c,d,sum:longint;
begin
read (a,b,c,d);
a:=a mod 23; b:=b mod 28; c:=c mod 33; sum:=a*5544+b* 14421+c*1288-d;
sum:=sum+21252; sum:=sum mod 21252; if (sum=0) then sum:=21252;
writeln(sum);
end.
输入:283 102 23 320
输出:
22.program program4;
var a:array [0..5] of integer;
sum,n,max,i,j,k:integer;
cover:array [0..22000] of Boolean;
begin
read(a[5],a[4],a[3],a[2],a[1],a[0]);
if ((a[5]=0) and (a[3]=0) and (a[1]=0))
then begin a[5]:=a[4]; a[4]:=a[2]; a[3]:=a[0]; a[2]:=0; a[0]:=0; end;
for i:=0 to 5 do if (a[i]>10) then a[i]:=10+(a[i] mod 2);
sum:=0;
for i:=0 to 5 do sum:=sum+a[i]*(6-i);
if ((sum mod 2)0) then begin writeln(‘Can’t be divided.’); exit; end;
sum:=sum div 2; max:=0; cover[0]:=True;
for i:=1 to sum*2 do cover[i]:=False;
for i:=0 to 5 do;
begin
j:=0;
while (j<a[i]) do
begin
for k:=max downto 0 do
begin if (cover[k]) then cover[k+6-i]:=True; end;
max:=max+6-I; j:=j+1;
end;
end;
if (cover[sum]) then writeln(‘Can be divided.’)
else writeln(‘can’t be divided.’);
end.
输入:4 7 9 20 56 48 输入:1000 7 101 20 55 1 输入:2000 5 l 1 0 0
输出: 输出: 输出:
23.program program1;
var
a,b,c,d,e:integer;
begin
a:=79; b:=34; c:=57; d:=0; e:=-1;
if (ac) then d:=d+e
else if (d+10<e) then d:=e+10
else d:=e-a;
writeln(d);
end.
24.program program2;
var
i,j:integer;
str1,str2:string;
begin
str1:='pig-is-stupid'; str2:='clever';
str1[1]:='d'; str1[2]:='o'; i:=8;
for j:=1 to 6 do begin str1[i]:=str2[j]; inc(i); end;
writeln(str1);
end.
25.program progam3;
var u:array [0..3] of integer;
a,b,c,x,y,z:integer;
begin
read(u[0], u[1], u[2], u[3]); a:=u[0]+u[1]+u[2]+u[3]-5;
b:=u[0]*(u[1]-u[2] div u[3]+8); c:=u[0]*u[1] div u[2]*u[3];
x:=(a+b+2)*3-u[(c+3) mod 4]; y:= (c*100-13) div a div (u[b mod 3]*5);
if((x+y) mod 2=0) then z:=(a+b+c+x+y) div 2;
z:=(a+b+c–x-y)*2; writeln(x+y-z);
end.
输入:2 5 7 4
输出: 。
26.program program4;
var c:array [1..3] of string[200];
s:array [1..10] of integer;
m,n,i:integer;
procedure numara;
var cod:boolean;
i,j,nr:integer;
begin
for j:=1 to n do begin
nr:=0; cod:=true;
for i:=1 to m do
if c[i,j]='1'
then begin if not cod
then begin cod:=true; inc(s[nr]); nr:=0; end
end
else begin
if cod then begin nr:=1; cod:=false; end
else inc(nr);
end;
if not cod then inc(s[nr]);
end;
end;
begin
readln(m, n); for i:=1 to m do readln(c[i]);
numara; for i:=1 to m do if s[i]0 then write(i,' ',s[i],' ');
end.
输入:3 10
1110000111
1100001111
1000000011
输出: 。
27.program cup_1;
var n,i,j,k,di,dj:integer;
a:array [1..20,1..20] of integer;
begin
n:=7;i:=1;j:=n;k:=1;di:=1;dj:=1;
while (k<=n*n) do
begin
a[i,j]:=k; k:=k+1;i:=i+di;j:=j+dj;
if (i=n+1)
then begin i:=n;j:=j-2;di:=-1;dj:=-1 end
else if (j=n+1)
then begin j:=n;di:=-1;dj:=-1 end
else if (j=0)
then begin i:=i+2;j:=1;di:=1;dj:=1 end
else if (i=0) begin j:=1;di:=1;dj:=1 end
end;
for j:=1 to n do write(a[n div 2,j]:4);
writeln
end.
28.program cup_2;
var s:string;i,j,m,w:integer;c:char;
a:array [0..127] of integer;
begin
for i:=0 to 127 do
begin
c:=chr(i);
if ((’0’<=c) and (c<=’9’))
then a[i]:=1
else
if (((’A’<=c) and (c<=’Z’)) or ((’a’<=c) and (c<=’z’)))
then a[i]:=2
else a[i]:=0
end;
readln(s); m:=length(s);
for i:=1 to m do
begin
j:=ord(s[i]);
if (a[j]>0) then begin write(s[i]);a[j]:=a[j]-1 end
end;
writeln
end.
输入:123+1234a+12abc-aaABB*ABC
输出:
29.program cup_3;
function f(m,n,k:integer):integer;
begin
if ((m=0) and (n=0) and (k=0))
then f:=1
else if ((0<=m) and (m<=n) and (n<=k))
then f:=f(m-1,n,k)+f(m,n-1,k)+f(m,n,k-1)
else f:=0;
end;
begin
writeln(f(3,3,3));
end.
30.program cup_4;
var s:string;wc,lcs,i:integer
begin
readln(s);wc:=0;lcs:=1;
for i:=1 to length(s) do
if ((lcs=0) and ((s[i]=’’) or (s[i]=chr(9))))
then lcs:=1
else if ((lcs=0) and (s[i]’’) and (s[i]chr(9)))
then begin wc:=wc+1;lcs:=0 end;
writeln(wc);
end
输入:This is a test program!
输出:
pascal 阅读程序
Program ex303;
Const
NN=7; //NN的值
Type
Arr1=array[0..30] of char;
var
s:arr1;
k,p:integer;
Function fun(s:arr1; a:char;n:integer):integer;
var
j:integer;
begin
j:=n; {j被赋成7,指向数组末尾}
while (a0) do dec(j);{如果查找字母比s[j]这个字母小,就把j减1,循环直到查找字母比s[j]大或等于,或者j=0}
fun:=j; {返回j的值}
end;
begin
for k:=1 to NN do
s[k]:=chr(ord('A')+2*k+1);{s[k]被赋成ASCII码为(字母A的ASCII码+2*k+1)的字母,执行完应是这样的 s=(D,F,H,J,L,N,P)}
k:=fun(s,'M',NN); {利用函数查找M按字母表顺序排列应在s数组中的位置}
writeln(k); {输出这一位置}
end.
pascal程序题(noip2012提高组初赛四3题) 求助 急急急~~~~
n值 数组有效元素(包括转换前和转换后)
i=2 1,1->2
i=3 2,1
i=4 2,1,1->2,2->4
i=5 4,1
i=6 4,1,1->4,2
i=7 4,2,1
i=8 4,2,1,1->4,2,2->4,4->8
至此次转换次数(->)为7次,即ans=7
i=9 8,1
i=10 8,1,1->8,2
i=11 8,2,1
i=12 8,2,1,1->8,2,2->8,4
i=13 8,4,1
i=14 8,4,1,1->8,4,2
i=15 8,4,2,1
i=16 8,4,2,1,1->8,4,2,2->8,4,4->8,8->16
由上可知,数组有效元素的和为n,当n可以表示为2(x)时,转换次数为2(0)+2(1)+2(2)+2(3)+...+2(x-1)
当i从1变到1024时,转换次数为2(0)+...+2(9)=1023
当i从1变到2048时,转换次数为2(0)+...+2(10)=2047,但是i变不到2048,只变到2012(n的值)
所以,最终数组的有效元素是:
i=20121024,512,256,128,64,16,8,4//有效数组元素的和为2012
所以,当n=2012时,最终的结果是:
t(1024)+t(512)+t(256)+t(128)+t(64)+t(16)+t(8)+t(4)//t(x)表示从数组初始状态变到x状态
=1023+511+255+127+63+15+7+3=2004
2012年noip提高组初赛答案对一下,刚刚考的
NOIP2012初赛提高组参考答案(PASCAL)
一、单项选择题
12345678910ABBADAADAB
二、不定项选择题
1 2 3 4 5 6 7 8 9 10
A AD AD BD ABC CD AB A CD BD
三、问题求解
1、 256
2、 5536
四、阅读程序题
1.41 (去除一个最大值和最小值后的平均值——取整)
2.16 (120的因子数)
3.(1)7 (2)2004 (尚不知程序到底要做什么)
4.55 (由先序遍历、中序遍历的序列构造出二叉树,再求出节点对应值的和——ABCDEF基本值分别对应123456,求出的是各个节点基本值乘以该节点深度的和)
五、完成程序题
1、不用递归直接使用循环输出P(N,M)种组合
(1)false (2) used[data[i]]:=false; (3)j (4)n (5)break
2、完善特殊栈的入栈、出栈、栈顶C个元素翻转操作
(1) next:=k mod c+1 (2) s[n]:=q[tail]; (3) q[head]
(4) q[head] (5) q[tail] (6) next(head)
同学考得不错呀