Advertisement
dyamondz

Cens (1) - X92517

Dec 30th, 2017
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. struct Persona{
  7.     string nom;
  8.     int edat;
  9. };
  10.  
  11. bool es_primer(int x){
  12.     if(x<2) return false;
  13.     for(int i=2;i*i<=x;++i){
  14.         if(x%i==0) return false;
  15.     }
  16.     return true;
  17. }
  18. // Post: Returns a vector v of 151 positions (0 to 150), where:
  19. //    v[i] is true if 'i' is a prime number,
  20. //    v[i] is false otherwise
  21. vector<bool> precompute_primes(vector<Persona>& p){
  22.     // Add your code here
  23.     vector<bool> pri(p.size());
  24.     for(int i=0;i<p.size();++i){
  25.         if(es_primer(p[i].edat)) pri[i]=true;
  26.         else pri[i]=false;
  27.     }
  28.     return pri;
  29. }
  30.  
  31. vector<bool> mateix_inicial(vector<Persona>& p, string& ciutat){
  32.     vector<bool>ini(p.size());
  33.     for(int i=0;i<p.size();++i){
  34.         if(ciutat[0]==p[i].nom[0]) ini[i]=true;
  35.         else ini[i]=false;
  36.     }
  37.     return ini;
  38. }
  39.  
  40. void introduir(vector<Persona>& p){
  41.     for(int i=0;i<p.size();++i){
  42.         cin>>p[i].nom>>p[i].edat;
  43.     }
  44. }
  45.  
  46. int main() {
  47.     // Add your code here ...
  48.     string ciutat;
  49.     bool primer=true;
  50.     while(cin>>ciutat){
  51.         if(!primer) cout<<endl;
  52.         primer=false;
  53.         int n;
  54.         cin>>n;
  55.         vector<Persona>p(n);
  56.         introduir(p);
  57.         vector<bool> pri=precompute_primes(p);
  58.         vector<bool> ini=mateix_inicial(p,ciutat);
  59.         cout<<ciutat<<endl;
  60.         for(int i=0;i<n;++i){
  61.             if(pri[i]){
  62.                 if(ini[i]) cout<<p[i].nom<<' '<<p[i].edat<<endl;
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement