1.递推算法(常用级数、数列求和、二分法、梯形积分法、穷举法等);
* 1+1/2+1/3+1/4+...+1/n
clear
input '请输入n的值:N=' to n
s=0
for i=1 to n
s=s+1/i
endfor
?S
* 1 1 2 3 5 8 13.....
clear
input '请输入n的值:N=' to n
dimension a[n]
a[1]=1
a[2]=1
s=2
for i=3 to n
a[i]=a[i-1]+a[i-2]
s=s+a[i]
endfor
?S
*二分法解方程3x*x*x-4x*x+3x-6=0要求误差小于十的负六次方
clear
input 'x1=' to x1
input 'x2=' to x2
fx1=x1*((3*x1-4)*x1+3)-6
fx2=x2*((3*x2-4)*x2+3)-6
do while fx1*fx2 > 0
input 'x1=' to x1
input 'x2=' to x2
fx1=x1*((3*x1-4)*x1+3)-6
fx2=x2*((3*x2-4)*x2+3)-6
enddo
x0=(x1+x2)/2
fx0=x0*((3*x0-4)*x0+3)-6
do while abs(fx0)>=0.000001
x0=(x1+x2)/2
fx1=x1*((3*x1-4)*x1+3)-6
fx0=x0*((3*x0-4)*x0+3)-6
if((fx0*fx1)<0)
x2=x0
else
x1=x0
endif
enddo
? x0
*梯形积分法求函数f=x*x-5x在区间[a,b]上的积分*
clear
? txjf(1,10,10000)
function txjf
parameters a,b,n
h=(b-a)/n
s=0
for i=1 to n
fa=f(a+i*h)
fb=f(a+(i+1)*h)
s=s+(fa+fb)*h/2
endfor
return s
function f
parameter x
return x*x-5*x
现有面值为l元、2元和5元的钞票(假设每种钞票的数量都足够多),从这些
钞票中取出30张使其总面值为100元,问有多少种取法?输出每种取法中各种面额钞票的张数。
clear
for i=1 to 30
for j=1 to 30
for k=1 to 20
if i+j+k=30 and i+2*j+5*k=100
? '1元的'+str(i)+'张 '+'2元的'+str(j)+'张 '+'5元的'+str(k)+'张'
endif
endfor
endfor
Endfor
2.排序算法(选择法、冒泡法);
clear
input 'N=' to n
dimension a[n]
for i=1 to n
input 'a['+str(i,2,1)+']=' to a[i]
endfor
for i=1 to n-1
p=i
for j=i+1 to n
If a[j]p = j EndIf endfor if p<>i temp=a[i] a[i]=a[p] a[p]=temp endif endfor for i=1 to n a[i] endfor clear input '请输入n的值:N=' to n dimension a[n] for i=1 to n input '请输入第'+str(i)+'个数:'to a[i] endfor for i=1 to n for j=i+1 to n if a[i]>a[j] temp=a[i] a[i]=a[j] a[j]=temp endif endfor endfor for i=1 to n ?a[i] endfor 3.查找算法(顺序查找、折半查找); clear input 'N=' to n dimension a[n] for i=1 to n input 'a['+str(i,2,1)+']=' to a[i] endfor input '请输入要查找的数findnum=' to findnum for i=1 to n if a[i]=findnum exit endif endfor if i>n ? 'not found' else ? 'find' endif clear input '请输入n的值:N=' to n dimension a[n] for i=1 to n input '请输入第'+str(i)+'个数:'to a[i] endfor for i=1 to n for j=i+1 to n if a[i]>a[j] temp=a[i] a[i]=a[j] a[j]=temp endif endfor endfor start=1 iend=n input '输入要查找的数num=' to num do while .t. and start<=iend mid=ceiling((start+iend)/2) if a[mid]=num exit endif if a[mid] endif if a[mid]>num iend=mid-1 endif enddo if start<=iend 'find' else 'not found' endif 4.有序数列的插入、删除操作; clear input "N=" to n dimension a[100] for i=1 to n input 'a['+str(i,2,1)+']=' to a[i] endfor for i=1 to n for j=i+1 to n if a[i]>a[j] temp=a[i] a[i]=a[j] a[j]=temp endif endfor endfor input "insert num=" to num for i=1 to n if a[i]>num exit endif endfor for j=n to i step -1 a[j+1]=a[j] endfor a[i]=num for i=1 to n+1 ?a[i] Endfor clear input "N=" to n dimension a[100] for i=1 to n input 'a['+str(i,2,1)+']=' to a[i] endfor for i=1 to n for j=i+1 to n if a[i]>a[j] temp=a[i] a[i]=a[j] a[j]=temp endif endfor endfor input "Delete num=" to num for i=1 to n if a[i]==num exit endif endfor if i>n ? "not found" for i=1 to n ?a[i] Endfor else for j=i to n a[j]=a[j+1] endfor for i=1 to n-1 ?a[i] Endfor endif 5.初等数论问题求解的有关算法(最大数、最小数、最大公约数、最小公倍数、素数等); clear input '请输入个数:'to n dimension a[n] for i=1 to n input '请输入第'+str(i)+'个数:'to a[i] endfor maxnum=a[1] minnum=a[1] for i=2 to n if a[i]>maxnum maxnum=a[i] endif if a[i] endif endfor ? '最大数是'+str(maxnum)+'最小数是'+str(minnum) clear store 0 to m,n,t,temp input '请输入第一个数:'to m input '请输入第二个数:'to n if n n=m m=temp endif p=n*m do while m<>0 r=n%m n=m m=r enddo ?'最大公约数是:',n ?'最小公倍数是:',p/n clear for i=2 to 100 for j=2 to i if INT(i/j)=i/j exit endif endfor if j=i i endif endfor 6.矩阵的处理(生成、交换及基本运算); *求3行3列矩阵的对角线和 clear dimension a[3,3] for i=1 to 3 for j=1 to 3 input 'a['+str[i,1]+']['+str[j,1]+']=' to a[i,j] endfor endfor for i=1 to 3 for j=1 to 3 a[i,j] endfor ? endfor s=0 for i=1 to 3 s=s+a[i,i] endfor ?S *求n行n列矩阵的鞍点(在矩阵行中最大,列中最小的数)* clear input 'n=' to n dimension a[n,n] for i=1 to n for j=1 to n input 'a['+str[i,1]+']['+str[j,1]+']=' to a[i,j] endfor endfor for i=1 to n for j=1 to n a[i,j] endfor ? endfor for i=1 to n temp=a[i,1] k=1 for j=1 to n if a[i,j]>temp temp=a[i,j] k=j endif endfor for r=1 to n if a[r,k] endif endfor if r>n ? temp endif endfor 7.递归算法(阶乘、最大公约数等); clear input "N=" to n ?str(n)+"!="+str(jc(n)) function jc parameters k if k=1 return 1 else return k * jc(k-1) Endif clear input "N=" to n input "M=" to m ? f(n,m) function f parameters x,y if x x=y y=t endif do while x % y != 0 t = y y = x % y x = t enddo return y 8.字符串处理(插入、删除、连接和比较等)。 function insertstr parameters s1,n,s2 return substr(s1,1,n-1)+s2+substr(s1,n) function delstr1 parameters s,n return substr(s,1,n-1)+substr(s,n+1) function delstr2 parameters s,s1 if at(s1,s)>0 do while at(s1,s)>0 s=stuff(s,at(s1,s),len(s1),'') enddo return s else return s endif下载本文