VFP程式設計題型別總結

2021-10-21 19:40:46 字數 5913 閱讀 1358

程式設計題型別總結(vfp)

①求和類:

1. 計算s=1!+2!+3!+4!+。。。。。。+n!

s=0k=1

input "n=" to n

for i=1 to n

k=k*i

s=s+k

endfor

?s2.求

sum=0

for i=1 to 21 step 2

sum=sum+i^3

endfor

?」sum=」,sum

3.有一分數序列,求前20項之和

m=1n=2

sum=0

for i=1 to 20

term=n/m

sum=sum+term

x=nn=m+n

m=xterm=n/m

endfor

?」sum=」,sum

4.求sn=a+aa+aaa+aaaa……,其中,a是乙個數字,a的個數為n,a和n由鍵盤輸入

input "輸入 a 的值:" to a

input "輸入項數n:" to n

sn=0

b=afor i=1 to n

sn=sn+a

a=a*10+b

endfor

?"數列之和為:",sn

5. 求ex的台勞級數展開式的前n項之和(執行時輸入x=2.0,n=4)

input 「x=」 to x

input 「n=」 to n

sum=1

p=1for i=1 to n-1

p=p*i

t=x^i/p

sum=sum+t

endfor

?」sum=」,sum

6.求1*2+3*4+5*6+……21*22

sum=0

for i=1 to 21 step 2

sum=sum+i*(i+1)

endfor

?"和為=",sum (1892)

7.通過鍵盤輸入n,求下列級數的

和:1-1/2+1/3-1/4+…+1/n

input to n

s=0f=1

for i=1 to n

s=s+f*(-1)^(i-1)/i

endfor

?s8.計算連續自然數之和,並且顯示和剛大於1000的最後乙個自然數

● sn=1+2+3+4+…+n

store 0 to s,n

do while s<=1000

n=n+1

s=s+n

enddo

? n9.求1^2+4^2+7^2+…一直加到超過1000為止,輸出和數與項數。

i=1s=0

n=0do while s<=1000

s=s+i^2

i=i+3

n=n+1

enddo

?n?s

10.求1-100之間所有奇數之和

sum=0

for i=1 to 100 step 2

sum=sum+i

endfor

? 「sum=」,sum

10.輸出100到200之間所有能被5或7整除的整數,並輸出他們的和。

sum=0

for i=100 to 200

if mod(i,10)=5 and mod(i,7)=0

sum=sum+i

?iendif

endfor

?「sum=」,sum

11.程式設計輸出100~500之間能被7整除的奇數的個數。

n=0for i=100 to 500

if i%7=0 and i%2=1

n=n+1

endif

endfor

? 「n=」,n

12. 求1/(1*2)+1/(3*4)+1/(5*6)+……+1/(99*100)

sum=0

for i=1 to 99 step 2

sum=sum+1/(i*(i+1))

endfor

?"sum=",sum

13.求如下級數前n項之和(x和n的值由鍵盤輸入)

s=1input to x

input to n

for i=1 to n

s=s+x^i/i

endfor

?「sum=」,s

14.輸出3-100之間的所有素數,並求其和。

sum=0

for j=3 to 100

flag=.t.

for i=2 to sqrt(j)

if mod(j,i)=0

flag=.f.

exit

endif

endfor

if flag=.t.

?jsum=sum+j

endif

endfor

?"奇數之和是:",sum

②方程類

1.完成下面分段函式的計算,程式執行時使用者從鍵盤輸入乙個數值x,在螢幕上輸出y值。

input "請輸入x值:" to x

if x>0

y=x*x

else

if x=0

y=0else

y=-(x*x)

endif

endif

?y2.現有一元2次方程 ax^2+bx+c=0 其中a b c 是整數且a不等於0 若存在實根,則輸出「在實數範圍內有解」 若不存在實根則輸出「在實數範圍內無解」/求解

input to a

input to b

input to c

d=b*b-4*a*c

if d>=0

?"在實數範圍內有解"

else

?"在實數範圍內無解"

endif

/ input to a

input to b

input to c

d=b*b-4*a*c

if d>0

x1=(-b+sqrt(d))/(2*a)

x2=(-b-sqrt(d))/(2*a)

?x1?x2

else

if d>0

x=-b/(2*a)

?"x1=x2=",x

else

?"無解"

endif

endif

3.輸入乙個x的值,求y的值。y=

input to x

do case

case x<1

y=xcase x<10

y=2*x-1

case x>=10

y=3*x-11

endcase

?y③陣列類

1. 輸入10個數,輸出其中小於平均值s的數。

dime a(10)

s=0for i=1 to 10

input to a(i)

s=s+a(i)

endfor

s=s/10

for i=1 to 10

if a(i)??a(i)

endif

endfor

2.輸入10個數,求其中最大數和最小數。

dime a(10)

for i=1 to 10

input 「 enter a number:」to a(i)

endfor

big=a(1)

small=a(1)

for i=2 to 10

if a(i)>big

big=a(i)

endif

if a(i)small=a(i)

endif

endfor

?「the smallest number is」,small

?「the biggest number is」,big

3.輸入10個數,求其中最大數和最小數,並確定它們的位置。

dime a(10)

for i=1 to 10

input 「 enter a number:」to a(i)

endfor

big=a(1)

bp=1

small=a(1)

sp=1

for i=2 to 10

if a(i)>big

big=a(i)

bp=i

endif

if a(i)small=a(i)

sp=i

endif

endfor

?「the place of the smallest number is」,sp

?「the smallest number is」,small

?「the place of the biggest number is」,bp

?「the biggest number is」,big

4.從鍵盤輸入10 個整數,現從中查詢某個數,若查到,輸出「yes」,否則輸出「no」。

dimension a(10)

for i=1 to 10

input "輸入10個整數" to a(i)

endfor

input "輸入待查詢的整數" to x

for i=1 to 10

if x=a(i)

yes"

exit

endif

endfor

if i>10

? "no"

endif

5.已有按從小到大的順序排列的10 個數,現輸入乙個數,要求插入到數列中。插入後數列仍然按從小到大順序排列。

dimension a(11)

for i=1 to 10

input 「遞增輸入10個整數」 to a(i)

endfor

input 「任意輸入乙個整數」 to x

for i=10 to 1 step -1

if x if i=1

a(i+1)=a(i)

a(i)=x

else

a(i+1)=a(i)

endif

else

a(i+1)=x

exit

endif

endfor

for i=1 to 11

??a(i)

endfor

?6. 陣列a中已存放了10個由小到大排列的數。現從鍵盤輸入乙個數x,要求刪除陣列中與其相同的數,並將後續的數依次前移,最後輸出剩餘的數。(7分)

考慮到有重複數的程式如下:

dime a(10)

for i=1 to 10

input 「按遞增輸入10個數」 to a(i)

endfor

input 「輸入待刪除的數」 to x

i=1j=1

n=0do while i<=10

if a(i) i=i+1

j=j+1

else

if a(i)=x

i=i+1

n=n+1

else

exit

endif

endif

enddo

for k=i to 10

a(j)=a(k)

j=j+1

endfor

for k=1 to 10-n

?a(k)

endfor

7.輸入10個數,按公升序排列後輸出。

dime a(10)

for i=1 to 10

input 「 enter an integer :」 to a(i)

endfor

for i=1 to 9

for j=i+1 to 10

if a(i)>a(j)

temp=a(i)

a(i)=a(j)

a(j)=temp

endif

安徽專公升本VFP程式設計題型別總結

求和類 1.計算s 1!2!3!4!n s 0k 1 input n to n for i 1 to n k k i s s k endfor s2.求 sum 0 for i 1 to 21 step 2 sum sum i 3 endfor sum sum 3.有一分數序列,求前20項之和 m ...

程式設計題型別總結

求和類 1.計算s 1!2!3!4!n s 0k 1 input n to n for i 1 to n k k i s s k endfor s2.求 sum 0 for i 1 to 21 step 2 sum sum i 3 endfor sum sum 3.有一分數序列,求前20項之和 m ...

程式設計題型別總結

求和類 1.計算s 1!2!3!4!n s 0k 1 input n to n for i 1 to n k k i s s k endfor s2.求 sum 0 for i 1 to 21 step 2 sum sum i 3 endfor sum sum 3.有一分數序列,求前20項之和 m ...