https://www.luogu.com.cn/problem/P5722
- 我直接累加输出了
#include<iostream>
using namespace std;
int tot;
int main()
{
int n;cin >> n;
for(int i = 1;i <= n;i++)
tot+=i;
cout <<tot;
}
大佬
使用头文件中的accumulate(头指针,尾指针,初始值)函数
#include<cstdio>
#include<numeric>
using namespace std;
int n,a[1001];
int main()
{
scanf("%d",&n);//输入n
for(int i=1;i<=n;i++) a[i]=i;//将1~n存入数组
return !printf("%d",accumulate(a+1,a+n+1,0));//调用STL
} //结束awa