P5722 【深基4.例11】数列求和||||||accumulate(头指针,尾指针,初始值)函数

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
发布了395 篇原创文章 · 获赞 52 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/dghcs18/article/details/104338467