set talk on clear set talk off return

  • Slides: 90
Download presentation

程序中可省代码 set talk on clear && 此句一般保留 …… set talk off return 5

程序中可省代码 set talk on clear && 此句一般保留 …… set talk off return 5

FOR. . . ENDFOR | NEXT 结构 for i=… to … step … ……

FOR. . . ENDFOR | NEXT 结构 for i=… to … step … …… if … …… (loop、exit) endif …… endfor 7 计数循环!

精确编程(不细讲) clear n=0 s=0 do while s<1000 n=n+2 s=s+n * ? n, s enddo

精确编程(不细讲) clear n=0 s=0 do while s<1000 n=n+2 s=s+n * ? n, s enddo ? s-n 17

非精确编程 clear s=0 for n=2 to 100 step 2 s=s+n ? n, s endfor

非精确编程 clear s=0 for n=2 to 100 step 2 s=s+n ? n, s endfor 18

求1~108所有整数的平方和。 #425754 求s=1*2+2*3+…+100*101 求s=1*3+3*5+…+99*101 可在Excel中求解 20

求1~108所有整数的平方和。 #425754 求s=1*2+2*3+…+100*101 求s=1*3+3*5+…+99*101 可在Excel中求解 20

已知S 1=1, S 2=1+2, S 3=1+2+4, …,求 S=S 1+S 2+S 3+S 4+…+S 20的值。 #2097130

已知S 1=1, S 2=1+2, S 3=1+2+4, …,求 S=S 1+S 2+S 3+S 4+…+S 20的值。 #2097130 24

clear dime f(20) f(1)=1 f(2)=1 ? f(1), f(2) for n=3 to 20 f(n)=f(n-2)+f(n-1) ?

clear dime f(20) f(1)=1 f(2)=1 ? f(1), f(2) for n=3 to 20 f(n)=f(n-2)+f(n-1) ? ? f(n) endfor 29

斐波那契数列{f(i)}: 1, 1, 2, 3, 5, 8, … 求F(45)值。 求F(1)+F(2)+…+F(50) 求F(1)+F(3) +…+F(49) 求10000000内最大的 求10000000内的个数

斐波那契数列{f(i)}: 1, 1, 2, 3, 5, 8, … 求F(45)值。 求F(1)+F(2)+…+F(50) 求F(1)+F(3) +…+F(49) 求10000000内最大的 求10000000内的个数 30 #1134903170 #32951280098 #12586269025 #9227465 #35

b=mod(int(x/100), 10) c=mod(int(x/10), 10) 或 b=int(mod(x, 1000)/100) c=int(mod(x, 100)/10) 37

b=mod(int(x/100), 10) c=mod(int(x/10), 10) 或 b=int(mod(x, 1000)/100) c=int(mod(x, 100)/10) 37

clear && 水仙花程序 1 n=0 for x=100 to 999 a=int(x/100) b=int((x-a*100)/10) c=mod(x, 10) if

clear && 水仙花程序 1 n=0 for x=100 to 999 a=int(x/100) b=int((x-a*100)/10) c=mod(x, 10) if x=a*a*a+b*b*b+c*c*c n=n+1 ? n, x endif endfor 38

clear && 水仙花程序 2 n=0 for a=1 to 9 for b=0 to 9 for

clear && 水仙花程序 2 n=0 for a=1 to 9 for b=0 to 9 for c=0 to 9 x=100*a+10*b+c if x=a*a*a+b*b*b+c*c*c ? ? x n=n+1 endif endfor ? n 39

clear &&回文数程序 1 n=0 for x=123 to 4321 a=int(x/1000) b=int((x-a*1000)/100) c=int((x-a*1000 -b*100)/10) d=mod(x, 10)

clear &&回文数程序 1 n=0 for x=123 to 4321 a=int(x/1000) b=int((x-a*1000)/100) c=int((x-a*1000 -b*100)/10) d=mod(x, 10) if (a=0 and b=d) or (a<>0 and a=d and b=c) n=n+1 ? ? x endif endfor ? n 42

clear && 回文数程序 2 n=0 for a=0 to 4 for b=0 to 9 for

clear && 回文数程序 2 n=0 for a=0 to 4 for b=0 to 9 for c=0 to 9 for d=0 to 9 x=1000*a+100*b+10*c+d if x>=123 and x<=4321 and ( (a=0 and b=d) or (a<>0 and a=d and b=c)) ? ? x n=n+1 endif endfor ? n 43

clear n=0 for x=100 to 200 k=0 for i=1 to x if mod(x, i)=0

clear n=0 for x=100 to 200 k=0 for i=1 to x if mod(x, i)=0 k=k+1 endif endfor if mod(k, 2)=1 ? ? x n=n+1 endif endfor ? n 45

set talk off clear k=0 for n=10 to 1000 s=0 for i=1 to n/2

set talk off clear k=0 for n=10 to 1000 s=0 for i=1 to n/2 if mod(n, i)=0 s=s+i endif endfor if s=n k=k+n endif endfor ? k set talk on return 47

程序(三重循环) clear n=0 for x=1 to 36 for y=1 to 36 for z=1 to

程序(三重循环) clear n=0 for x=1 to 36 for y=1 to 36 for z=1 to 36 if x+y+z=36 and 4*x+2*y+z=120 n=n+1 ? x, y, z endif endfor ? n 54

clear for c=121 to 140 for b=2 to c-1 for a=1 to b-1 if

clear for c=121 to 140 for b=2 to c-1 for a=1 to b-1 if a*a+b*b=c*c ? a, b, c endif endf 59

程序 a=113 b=355 s=0 for i=1 to 30 a=a*10 q=int(a/b) * ? ? str(q,

程序 a=113 b=355 s=0 for i=1 to 30 a=a*10 q=int(a/b) * ? ? str(q, 1) s=s+q a=mod(a, b) endfor ? q, s return 63

clear n=0 for x=1 to 9999 do case x<10 m=10 case x<100 m=100 case

clear n=0 for x=1 to 9999 do case x<10 m=10 case x<100 m=100 case x<1000 m=1000 case x<10000 m=10000 endcase if mod(x*x, m)=x ? x, x*x n=n+1 endif endfor ? n 65

clear n=0 for x=1 to 1000 y=int(sqrt(x)) if x=y*y ? ? x n=n+1 endif

clear n=0 for x=1 to 1000 y=int(sqrt(x)) if x=y*y ? ? x n=n+1 endif endfor ? n 67

clear x=98765432 p=2 n=0 do while x>0 r=mod(x, p) if r=1 n=n+1 endif ?

clear x=98765432 p=2 n=0 do while x>0 r=mod(x, p) if r=1 n=n+1 endif ? p, x, r x=int(x/p) enddo ? str(x, 20) ? n 程序 69

程序 clear x=0. 5432 p=2 ? x for n=1 to 15 y=p*x ? y

程序 clear x=0. 5432 p=2 ? x for n=1 to 15 y=p*x ? y z=int(y) x=y-z endfor ? z 71

使用自定义内部逻辑函数prime(x) function prime parameters x 牢 private i 记 if x<2 在 return. f.

使用自定义内部逻辑函数prime(x) function prime parameters x 牢 private i 记 if x<2 在 return. f. 心 endif 73 for i=2 to x-1 if mod(x, i)=0 return. f. endif endfor return. t.

clear n=0 for x=1 to 100 if prime(x) ? ? x n=n+1 endif endfor

clear n=0 for x=1 to 100 if prime(x) ? ? x n=n+1 endif endfor ? n return function prime parameters x private i if x<2 return. f. endif for i=2 to x-1 if mod(x, i)=0 return. f. endif endfor return. t. 75

解答 c= 6744 n=0 for a=3 to c/2 step 2 b=c-a if prime(a) and

解答 c= 6744 n=0 for a=3 to c/2 step 2 b=c-a if prime(a) and prime(b) n=n+1 endif endfor ? n && 后接函数 78

for a=200 to 1000 -2 if prime(a) and prime(a+2) s=2*a+2 ? a, a+2, s

for a=200 to 1000 -2 if prime(a) and prime(a+2) s=2*a+2 ? a, a+2, s endif next ? s && 后接函数 80

k=0 for n=100 to 200 -1 if prime(n*(n+1)-1) k=k+1 endif next ? k &&

k=0 for n=100 to 200 -1 if prime(n*(n+1)-1) k=k+1 endif next ? k && 后接函数 82

程序 clear n=0 for x=1000 to 9999 y=x-int(x/1000)*1000 z=x-int(x/100)*100 w=x-int(x/10)*10 if not('0'$str(x)) and prime(x)

程序 clear n=0 for x=1000 to 9999 y=x-int(x/1000)*1000 z=x-int(x/100)*100 w=x-int(x/10)*10 if not('0'$str(x)) and prime(x) and prime(y) and prime(z) and prime(w) ? ? x n=n+1 endif endf ? n && 后接函数 85

程序 clear n=0 for x=1000 to 9999 y=int(x/10) z=int(y/10) w=int(z/10) if not('0'$str(x)) and prime(x)

程序 clear n=0 for x=1000 to 9999 y=int(x/10) z=int(y/10) w=int(z/10) if not('0'$str(x)) and prime(x) and prime(y) and prime(z) and prime(w) ? ? x n=n+1 endif endf ? n && 后接函数 88