编写一程序,求1+2+3+……的前N项和,直到其和刚刚1000止,将N值保存在DX中,累加和保存在AX中

来源:百度知道 编辑:UC知道 时间:2024/05/24 17:22:11
用两种方法实现

1
for n = 1 to 500
if ax > 1000 then
exit for
else
ax = ax + n
end if
next
dx = n

2
do while dx > 1000
dx = dx + 1
ax = ax + dx
loop

楼主,这个问题根本就无解呀.你看哈DX=44时,AX=990;当DX=45时,AX=1035.
不过如果把等于一千换成不大于一千且最接近一千,则有如下程序:
#include <stdio.h>
main()
{int DX=1,AX=0;
while (AX<=1000)
{AX=AX+DX;
DX++;}
AX=AX+1-DX;
DX=DX-2;
printf("DX=%d AX=%d",DX,AX);
getchar();
}