C++ :
#include <iostream>
#include <vector>
using namespace std;
bool check(int k){
for(int i = 2; i * i <= k; i++)
if(k % i == 0)
return false;
return true;
}
void solve(){
vector<int> a, res;
int n, sum = 0;
cin >> n;
a.resize(n);
for(int i = 0; i < n; i++){
cin >> a[i];
if(i == 0) continue;
if(check(i + 1))
res.push_back(a[i]);
else
sum += a[i];
for(auto &i : res) cout << i << " ";
cout << "\n" << sum;
signed main(){
solve();
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
C++ :
#include <iostream>
#include <vector>
using namespace std;
bool check(int k){
for(int i = 2; i * i <= k; i++)
if(k % i == 0)
return false;
return true;
}
void solve(){
vector<int> a, res;
int n, sum = 0;
cin >> n;
a.resize(n);
for(int i = 0; i < n; i++){
cin >> a[i];
if(i == 0) continue;
if(check(i + 1))
res.push_back(a[i]);
else
sum += a[i];
}
for(auto &i : res) cout << i << " ";
cout << "\n" << sum;
}
signed main(){
solve();
}