天天看点

求和到第几项

题目描述

例4.5 求s=1 +2 +3……+n,当加到第几项时,s的值会超过1000?

输入

无输入。

输出

一个整数n,表示求和到第n项。

#include<bits/stdc++.h>
using namespace std;
int s,i=1;
int main()
{
    while(s<=1000)
    {
        s+=i;
        i++;    
    }
    cout<<i-1;
    return 0;
}
           
上一篇: 工作班会

继续阅读