主题:总是11;怎么写s=1+(1+2)+(1+2+3+...+n)程序
hngnba
[专家分:0] 发布于 2012-02-02 16:12:00
[size=4]怎么写s=1+(1+2)+(1+2+3+...+n)程序,n为任意输入的数值,运行时在LABEL里写示出每一项的内容,例如我在TEXT里输入的N值为10,则运行时,LABEL里要显示出s=1+(1+2)+.....+(1+2+3+...+10)=220.请各位知道的朋友详细指教一下,谢谢!!![/size]
回复列表 (共7个回复)
沙发
esailor [专家分:2650] 发布于 2012-02-02 16:35:00
s = 0
for tmp_i = 1 to n
for tmp_j = 1 to tmp_i
s = s + tmp_j
endfor
endfor
板凳
hngnba [专家分:0] 发布于 2012-02-06 09:35:00
实现不了,具体点吗
3 楼
hngnba [专家分:0] 发布于 2012-02-06 09:36:00
实现不了,能具体点吗
4 楼
jch_168 [专家分:0] 发布于 2012-02-08 14:32:00
s = ''
for tmp_i = 1 to n
for tmp_j = 1 to tmp_i
s = s + iif(tmp_j=1 and tmp_i<>1,"+(",iif(tmp_i=1,"","+"))+ltrim(str(tmp_j))+iif(tmp_j=tmp_i and tmp_i<>1,")","")
endfor
endfor
5 楼
jch_168 [专家分:0] 发布于 2012-02-08 14:34:00
s="s="
for tmp_i = 1 to n
for tmp_j = 1 to tmp_i
s = s + iif(tmp_j=1 and tmp_i<>1,"+(",iif(tmp_i=1,"","+"))+ltrim(str(tmp_j))+iif(tmp_j=tmp_i and tmp_i<>1,")","")
endfor
endfor
6 楼
qjbzjp [专家分:8830] 发布于 2012-02-08 21:45:00
将1楼、5楼二位的代码加一起可能是楼主要的效果吧:
sn = 0
n=10
s="s="
FOR tmp_i = 1 to n
FOR tmp_j = 1 to tmp_i
sn = sn + tmp_j
s = s + iif(tmp_j=1 and tmp_i<>1,"+(",iif(tmp_i=1,"","+"))+ltrim(str(tmp_j))+iif(tmp_j=tmp_i and tmp_i<>1,")","")
NEXT
NEXT
?s+"="+TRANSFORM(sn)
7 楼
sdta [专家分:570] 发布于 2012-02-09 21:44:00
[color=008000]这才是楼主的意思:[/color]
s = 0
n=10
s1=""
for tmp_i = 1 to n
s1=s1+transform(tmp_i)+iif(tmp_i<n,"+","")
for tmp_j = 1 to tmp_i
s = s + tmp_j
endfor
endfor
thisform.label1.caption=s1+"="+transform(s)
我来回复