题解:P12600 连号子序列数

· · 题解

五年级蒟蒻题解,大佬见谅。

第一句:本题是P8600 [蓝桥杯 2013 省 B] 连号区间数的加强版一看就是唬人的,直接忽略。

思路

这个算一下就可以推出来:不同连号子序列的数目为 1 + 2 + 3 + ... + n = \frac{n\times(n+1)}{2}。

代码

三种代码都能过:

#include<bits/stdc++.h>//万能头
using namespace std;
using ll=long long;
ll n;
int main(){
    ios::sync_with_stdio(false);//速读
    cin.tie(0),cout.tie(0);
    cin>>n;
    cout<<n*(n+1)/2<<endl;
    return 0;//完美收尾
}
#include<bits/stdc++.h>
#define endl '\n'
#include<bits/stdc++.h>//万能头
using namespace std;
using ll=long long;
ll n,cnt;
int main(){
    ios::sync_with_stdio(false);//速读
    cin.tie(0),cout.tie(0);
    cin>>n;
    for(ll i=1,x;i<=n;i++){
        cin>>x;
        cnt+=x;
    }
    cout<<cnt<<endl;
    return 0;//完美收尾
}
#include<bits/stdc++.h>//万能头
using namespace std;
using ll=long long;
ll n,cnt;
int main(){
    ios::sync_with_stdio(false);//速读
    cin.tie(0),cout.tie(0);
    cin>>n;
    for(ll i=1;i<=n;i++){
        cnt+=i;
    }
    cout<<cnt<<endl;
    return 0;//完美收尾
}