If
Select case 分支 循环
1. do while 条件
2. for-简练, 终止值确定
3. S=1+2+3+..n,n由键盘输入 For
1. 初始化 2. 条件
3. 循环体-循环n语句 4. 条件的修改
Input “n=” to n 3 4 10 100 S=0
For i=1 to n S=s+i Endfor ?s
Return
4. S=1/1+1/2+1/3+1/4+…+1/n, 直到1/n小于10^(-8)为止 S=0 i=1
do while 1/i>=10^(-8) s=s+1/i*(-1)^(i+1) i=i+1 enddo ?s return
1/n<10^(-8)-停止 1/n>=10^(-8)-条件 For
Do while
5. S=1-1/2+1/3-1/4+…+1/n,
直到1/n小于10^(-8)为止
6. n!,n由键盘输入-for n!=1*2*3*…*n input “n=” to n s=1
for i=1 to n s=s*i endfor ?s return
7. s=1!+2!+…+n!, 盘输入-for
n由键
s=s+t
t=t*I i=1,2,3 input “n=” to n s=0 t=1
for i=1 to n i=1,2,3…
t=t*i=1*1=1!*2=2!*3=3! s=s+1/t=0+1!+2!+3! Endfor-i=i+1 ?s return
t=1!=1*1
2!=1*2=1!*2 …3!=1*2*3=2!*3 4!= 1*2*3*4=3!*4
N!=(n-1)*n
8. s=1+1/2!+1/3!…+1/n!, n由键盘输入 input “n=” to n s=0 t=1
for i=1 to n t=t*i s=s+1/t endfor ?s return
s=1+1/2!+1/3!…+1/n!
直到1/n!小于10^(-6)为止-条件-do while Input “x=” to x s=0 t=1 i=1
do while x^i/t>=10^(-6) s=s+x^i/t*(-1)^(i+1) i=i+1 t=t*i enddo ?s return
9. s=x^1/1!-x^/2!+x^3/3!-1/4!…+x^n/n!, 直到1/n!小于10^(-6)为止
10. s=x-x^2/2!+x^3/3!-x^4/4!…+x^n/n!, 直到x^n/n!小于10^(-6)为止,x由键盘
11. s=2!+4!+6! S=0 T=1
For i=2 to 6 step 2 i=2,4
T=t*(i-1)*i=1*1*2=2!*3*4
=4!
S=s+t=0+2!+4! Endfor ?s return
12. s=3!+5!+7! S=0 T=1
For i=3 to 7 step 2 i=3,5
T=t*(i-1)*i=1*2*3=3!*4*5=5!
S=s+t=0+3!+5! Endfor ?s
return
13. s=1/2!+1/4!+1/6!+…+1/n!, n由键盘输入 for
input “n=” to n s=0 t=1
for i=2 to n step 2 t=t*(i-1)*i s=s+1/t endfor ?s return
14. s=1/2!+1/4!+1/6!+…+1/n!, 直到1/n!小于10^(-6)
为止-do while input “x=” to x s=1 t=2 i=2 d=-1
do while x^i/t>=10^(-6) s=s+x^i/t*d i=i+2
t=t*(i-1)*i d=-d enddo ?s return
15. s=1+x^2/2!+x^4/4!+1/
6!+…+x^n/n! 16.
17. s=1-x^2/2!+x^4/4!-x^6/6!+…x^n/n!,
x^n/n!<10^(-8)为止 input “x=” to x s=1 t=2 i=2 d=-1
do while x^i/t>=10^(-8) s=s+x^i/t*d i=i+2
t=t*(i-1)*i d=-d
enddo ?s
Return
输出所有的水仙花数 100-999
For n=100 to 999 X=int(n/100)
Y=int(n%100/10) Z=n%10
If x^3+y^3+z^3=n then ?n Endif Endfor Return
判断某个数是否是素数 素数:质数,整数,只能被1和它本身整除,其余数据2-n-1都不能被整除。
1既不是质数也不是合数,2是最小的质数。
N-键盘输入 N:
2-n-1, 任意有一个被整除-no
2-n-1,都不能整除,yes
N%i=0 i=2-n-1 For n=100 to 1000 For i=2 to n-1 i=2 3 If 15%i=0 then
Exit Endif Endfor If i=n then ?”yes” Else ?”no” Endif Endfor Return
输出100-1000
的所有素
数。
For n=100 to 1000 For i=2 to n-1
If n%i=0 then Exit Endif Endfor If i=n then
?n Endif Endfor Return
以5个为一组输出1-100所有整数。
以5个为一组输出1-100
的中
所有能被3整数的数,统计总个数。
1) 输出1-100中所有能
被3整数的数 2) 统计总个数 3) 以5个为一组 C=0
For i=1 to 100 If i%3=0 then
??i C=c+1
If c%5=0 then ? endif Endif
Endfor
?”总个数:”,c return
以7个为一组输出100-1000的所有素数,并统计显示其总个数。
1) 输出100-1000的所有
素数
2) 统计总个数 3) 以7个为一组 分解-重要 C=0
For n=100 to 1000 For i=2 to n-1
If n%i=0 then
Exit Endif Endfor If i=n then
??n C=c+1
If c%7=0 then ? Endif Endif Endfor ?”总个数:”
C=0
For n=100 to 1000 For i=2 to n-1
If n%i=0 then Exit Endif Endfor If i=n then
??n C=c+1
If c%7=0 then ?
endif Endif Endfor
?”总个数:”,c return
For i=1 to 100 If i%3=0 then ? Endif endfor
4) 以5个为一组 3 6 9 12 15? C= 3 6 9 12 15?
C=5%5=0
18 21 24 27 30? …
For i=1 to 100 ?i Endfor Return
5 10 15 20 25 30… 1 2 3 4 5 ??i ?-换行 1 2 3 4 5? I%5=0 6 7 8 9 10? 11 12 13 14 15? …
百钱买百鸡。100钱买100只鸡,公鸡5元/只,母鸡3元/只,小鸡3只1元。问有多少只公鸡、母鸡和小鸡。 X Y Z
X+y+z=100 1 1 98
5x+3y+z/3=100 100 100
X:1-20 Y:1-33
Z:100-x-y For x=1 to 20 For y=1 to 33
Z=100-x-y
If 5x+3y+z/3=100 then ?x,y,z Endif Endfor Endfor return
1 1 98 5 3 92
计算分段函数
sinxx21x0y1x0cosxx33xx0 x从键盘输入。输出y的值。 Sin(x):正弦, x数值 Cos(x): Sqrt(x)
Input “x=” to x Do case Case x>0
Y=sin(x)+sqrt(x^2+1) Case x=0 Y=1 otherwise
y=cos(x)-x^3+3*x endcase ?y return
设计一个程序,要求是: 1) 从键盘输入一个不小
于3的自然数N(例如输入10),求其不到第n个自然数中奇数的和,并输出结果。 2) 输出1到第n个自然
数中所有质数-素数的个数。 Input “n=” to n If n>=3 then S=0
For i=1 to n-1 step 2 S=s+i Endfor
?”奇数的和:”,s C=0
For m=2 to n
For i=2 to m-1 If m%i=0 then Exit Endif
Endfor If i=m then C=c+1 Endif Endfor
?”质数的个数:”,c Else
?”请输入一个不小于的数” Endif return
S=1+2+..+100
3
Input “n=” to n If n>=3 then S=0
For i=1 to n-1 step 2 S=s+i Endfor
?”奇数的和:”,s C=0
For m=2 to n
For i=2 to m-1 If m%i=0 then Exit Endif Endfor If i=m then
C=c+1 Endif Endfor
?”质数总个数:”,c Else
?”请输入一个不小于3的数” Endif return
已知2010年中国GDP为120万亿,美国的GDP为140万亿,假设从2011年
开始中国的GDP增长率为10%,美国为6%,请问多少年后中国的GDP能超过美国。 推导
计算1到100中每个数据的立方根(数据总长度为10,小数位数为7),将结果保存到外存,文件名为root.c.
Y%4=0 and y%100<>0
5:2,3,4, yes 6: 2,3,4,5, no 13: 2-12, yes 15: 2,3,…14, no 15121: 2-15120
3 6 9 12 15? C=5%5=0
1 1 98
1 2 97
1 3 96 5*1+3*3+96/3?=100 … 2 1 97 2 2 96 …
20 1 79 20 2 78 …
20 33 47 Z:3
For x=1 to 20 For y=1 to 33 Z=100-x-y
If z%3=0 and 5*x+3*y+z/3=100 then ?”公鸡”,x ?”母鸡”,y ?”小鸡”,z Endif Endfor Endfor I j
1*1=1 1*2=2 1*3=3 1*4=4 2*1=2 2*2=4 2*3=6 2*4=8 3*1=1 3*2=2 3*3=3 3*4=4 4*1=1 4*2=2 4*3=3 4*4=16
I=1,2,3,4
I=1 J=1 to 4 I=2 J=2 to 4 I=3 j=3 to 4
I=4 j=4 to 4 j=i to 4 Str(I,1) * str(j,1) = str(i*j,2) space(5)
1 1 1 1 2 5
Space(9)
1*1=1 1*2=2 1*3=3 1*4=4
2*2=4 2*3=6 2*4=8
3*3=3 3*4=4
4*4=16
1*1=1 1*2=2 1*3=3 1*4=4 1*i= i=1,2,3,4 For i=1 to 4 ?1*i endfor
2*1=2 2*2=4 2*3=6 2*4=8
For i=1 to 4 i=1,2 For j=1 to 4 j=1,2,3,4
??i,i*j Endfor ? endfor str(x,m,n):
将数值型数据x转换为字符串,总长度m,小数位数n,没有n,n=0 Str(I,1) 字符串:+ For i=1 to 4 For j=1 to 4
??str(I,1)+”*”+str(j,1)+”=”+str(i*
j,2)+space(3)
Endfor ? Endfor
九九乘法表右半部分。 For i=1 to 9 For j=i to 9
??str(I,1)+”*”+str(j,1)+”=”+st
r(i*j,2)+space(3)
Endfor ?space(9*i) Endfor
最大公约数和最小公倍数。
最大公约数
最小公倍数=m*n/最大公约数
M=60 60*20/20=60 N=20 20 M=30 N=20
30*20/10=60
算法:m,n(m>n),取m和n的余数r,如果r不为0,就将n的值赋给m,将r的值赋给n,继续取m和n的余数r,
直到r为0为止,-停 r不为0,进入循环 -do while r<>0
n就是最大公约数。 1)m,n键盘 2) r=m%n 3) 当r<>0,m=n,n=r, r=m%n,反复执行
4) 直到r=0为止, n-最大公
约数
Input “m=” to m Input “N=” to n X=m Y=n R=m%n
Do while r<>0 M=n N=r R=m%n Enddo ?n ?x*y/n Return
Input “m=” to m=30 Input “n=” to n=20 R=m%n=10 Do while r<>0 M=n=20 N=r=10
R=m%n=20%10=0 Enddo ?n=10 ?m* Return
最小公倍数
Input “m=” to m Input “n=” to n
X=m Y=n R=m%n
Do while r<>0 M=n N=r R=m%n Enddo
?”最大公约数:”,n ?”最小公倍数:”,x*y/n return
60 20 5 10 20 30 20
5 10
因篇幅问题不能全部显示,请点此查看更多更全内容