ABC479-A Odd Index Sumの解説
- diff12(灰色)
- Solve Time 3mins
- これは秒で解きたい
問題の概要
長さが$N$の$A=(A_1,A_2,A_3,...,A_N)$があるので、奇数番目の値を足した値を求めてください。
最初に思ったこと
- やるだけですね
発想
1idxでやると奇数番目にしっかりなってくれる!
実装
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define el '\n'
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,l,n) for(int i=l;i<n;i++)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> using pq = priority_queue<T>;
template<class T> using pq_ = priority_queue<T,vector<T>,greater<T>>;
template<class T>inline bool chmin(T&a,T b){if(a<=b){return false;}else{a=b;return true;}};
template<class T>inline bool chmax(T&a,T b){if(a>=b){return false;}else{a=b;return true;}};
const int dr4[4]={0,-1,0,1};const int dc4[4]={-1,0,1,0};
const int dr8[8]={-1,-1,-1,0,1,1,1,0};const int dc8[8]={-1,0,1,1,1,0,-1,-1};
//左(左上)から時計回り
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;cin >> N;
vector<int> vec(N+1);
int sum=0;
REP(i,1,N+1){
int a;cin >>a;
if(i%2==1){
sum+=a;
}
}
cout << sum<<el;
return 0;
}
参考