C++阅读程序,说明功能?

C++阅读程序,说明功能?,第1张

函数fun的作用里用递归是逆序输出传入的非负整数

递归结束条件式n/10==0,即n>=0&&n<10

否则就先进入下一次递归,然后输出n%10对应的字符

也就实现了逆序输出的效果

我以为多做就是最好的技巧,给你30道

1.请将以下程序段表示的计算公式写出来(假设X的值已给出)

E:=1A:=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:=X2X2:=X

END

WRITELN(‘X=’,X)

3.阅读下列程序段,写出程序运行后数组元素A1,A2,…,A11中的值。

A[1]:=1

A[2]:=1K:=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]:=kk:=k+1 end

else for j: =1 to i do begin a[i-j+1,j]:=kk:=k+1end

else if odd(i) then for j:=n downto i-n+1 do

begin a[i-j+1,j]:=kk:=k+1end

else for j:=i-n+1 to n do

begin a[i-j+1,j]:=kk:=k+1end

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:=nfor j1:=(n-1) downto (n-i1+1) do s1:=s1*j1 div (n-j1+1)

co:=s1

end

begin

s:=n+1for 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]:=0p[n+1]:=100for 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:=0for i:=0 to 100 do if g[i]>0 then begin write(i,:4)s:=s+1end

writelnwriteln(‘s=’,s)writeln(‘input data:’)readln(x)

writeln(g[x])

end.

输入:10 2065

input data: 10

输出:

8.program excpl

var x,y,y1,jk,j1,g,e:integcr

a:array [l..20] of 0..9

begin

x:=3465y:=264jk:=20for j1:=1 to 20 do a[j1]:=0

while y<>0 do

begin

y1:=y mod 10y:=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 10g:=g div 10 end

y1:=y1-1

end

jk:=jk-1

end

j1:=1while 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:=211for i:=1 to 14 do begin a[i]:=ij:=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:=lk:=1t:=l

while s<n do begin k:=k+1t:=t*ls:=s+t end

s:=s-tn:=n-s-1for i:=1 to 10 do b[i]:=0

j:=11while n>0 do begin j:=j-1b[j]:=n mod ln:=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:=1p1:=trueb[j]:=qj1:=0

while (q>0) and p1 do

begin

j1:=j1+1c[j1]:=q*10 div pq:=q*10-c[j1]*p

if q>0 then begin

j2:=1while (b[j2]<>q) and (j2<=j) do j2:=j2+1

if b[j2]=q

then begin

p1:=falsewrite('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+1b[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:=0s:=(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:=0p:=true

while p do

begin

i1:=i1+1s:=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-1lk:=n-1

for i:=1 to n-1 do

begin

l1:=l0-kif (l1<0) then l1:=l1+n

if (l1=lk) then begin a[l0]:=a[n]lk:=lk-1a[n]:=a[lk]l0:=lk end

else begin a[l0]:=a[l1]l0:=l1end

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:=1jw:=njb:=n:

while (jr<=jw) do

begin

if (ch[jw]='R')

then begin

ch1:=ch[jr]ch[jr]:=ch[jw]ch[jw]:=ch1jr:=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-1jb:=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-1a[j]:=n mod 10n:=n div 10end

s:=0for i:=j t0 20 do s:=s*p+a[i]writeln(s)j:=21

while (s>o)do begin j:=j-1a[j]:=s mod qs:=s div qend

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:=100x:=l0y:=20okl:=5ok2:=0

if ((x>y) or ((y<>20) and (okl=0)) and (ok2<>0)) then a:=1

else if ((okl<>0) and (ok2=、0)) then a:=-1 else a:=0

writeln(a)

end.

20.program Program2

var a,t:string

i,j:integer

begin

a:=morningj:=lfor i:=2 to 7 do if (a[j]<a[i]) then j:=i

j:=j-1for 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 23b:=b mod 28c:=c mod 33sum:=a*5544+b* 14421+c*1288-d

sum:=sum+21252sum:=sum mod 21252if (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]:=0a[0]:=0end

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.’)exitend

sum:=sum div 2max:=0cover[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]:=Trueend

max:=max+6-Ij:=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:=79b:=34c:=57d:=0e:=-1

if (a<c) or (b>c) 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)*2writeln(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:=0cod:=true

for i:=1 to m do

if c[i,j]='1'

then begin if not cod

then begin cod:=trueinc(s[nr])nr:=0end

end

else begin

if cod then begin nr:=1cod:=falseend

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])

numarafor 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:=7i:=1j:=nk:=1di:=1dj:=1

while (k<=n*n) do

begin

a[i,j]:=kk:=k+1i:=i+dij:=j+dj

if (i=n+1)

then begin i:=nj:=j-2di:=-1dj:=-1 end

else if (j=n+1)

then begin j:=ndi:=-1dj:=-1 end

else if (j=0)

then begin i:=i+2j:=1di:=1dj:=1 end

else if (i=0) begin j:=1di:=1dj:=1 end

end

for j:=1 to n do write(a[n div 2,j]:4)

writeln

end.

28.program cup_2

var s:stringi,j,m,w:integerc: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:stringwc,lcs,i:integer

begin

readln(s)wc:=0lcs:=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+1lcs:=0 end

writeln(wc)

end

输入:This is a test program!

输出:

递归你需要先找到递归的递归条件和终结条件。

递归的基本规则可以解释为:

如果条件为继续递归

为条件做一定变换后调用自身

否则

返回基本值

当然,可能会出现更复杂的,但一般都是这样。所以需要找到三个地方:

第一、就是在什么条件下要继续递归,在什么条件下结束递归。

第二、递归时需要做什么变换才能让递归函数最终走到终结条件。

第三、基本值是什么。

比如递归计算N!。我们知道函数基本写作

int func(int n)

{

    if(n > 1)

        return n * func(n - 1)

    else

        return 1

}

针对这个函数,三点就是:

第一、递归条件就是只要n不等于1,则递归继续,否则递归终结。

第二、递归变换就是n-1。

第三、终结时,基本值是1。

更复杂的递归也是符合这些条件的,只不过可能不止一种变换或不止一种终结条件等等。

求采纳哦


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

原文地址: http://outofmemory.cn/yw/8032680.html

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

发表评论

登录后才能评论

评论列表(0条)

保存