Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- using namespace std;
- struct Persona{
- string nom;
- int edat;
- };
- bool es_primer(int x){
- if(x<2) return false;
- for(int i=2;i*i<=x;++i){
- if(x%i==0) return false;
- }
- return true;
- }
- // Post: Returns a vector v of 151 positions (0 to 150), where:
- // v[i] is true if 'i' is a prime number,
- // v[i] is false otherwise
- vector<bool> precompute_primes(vector<Persona>& p){
- // Add your code here
- vector<bool> pri(p.size());
- for(int i=0;i<p.size();++i){
- if(es_primer(p[i].edat)) pri[i]=true;
- else pri[i]=false;
- }
- return pri;
- }
- vector<bool> mateix_inicial(vector<Persona>& p, string& ciutat){
- vector<bool>ini(p.size());
- for(int i=0;i<p.size();++i){
- if(ciutat[0]==p[i].nom[0]) ini[i]=true;
- else ini[i]=false;
- }
- return ini;
- }
- void introduir(vector<Persona>& p){
- for(int i=0;i<p.size();++i){
- cin>>p[i].nom>>p[i].edat;
- }
- }
- int main() {
- // Add your code here ...
- string ciutat;
- bool primer=true;
- while(cin>>ciutat){
- if(!primer) cout<<endl;
- primer=false;
- int n;
- cin>>n;
- vector<Persona>p(n);
- introduir(p);
- vector<bool> pri=precompute_primes(p);
- vector<bool> ini=mateix_inicial(p,ciutat);
- cout<<ciutat<<endl;
- for(int i=0;i<n;++i){
- if(pri[i]){
- if(ini[i]) cout<<p[i].nom<<' '<<p[i].edat<<endl;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement