JosepRivaille

P34438: Països i províncies (2)

May 7th, 2015
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. struct Provincia {
  7.     string nom;
  8.     string capital;
  9.     int habitants;
  10.     int area;
  11.     double pib;
  12. };
  13.  
  14. struct Pais {
  15.     string nom;
  16.     string capital;
  17.     vector<Provincia> provs;
  18. };
  19.  
  20. typedef vector<Pais> Paisos;
  21.  
  22. int habitants(const Paisos& p, double x) {
  23.     int tam_p = p.size();
  24.     int suma_hab = 0;
  25.     for (int i = 0; i < tam_p; ++i) {
  26.         int sum_provs_less_x = 0;
  27.         int tam_provs = p[i].provs.size();
  28.         for (int j = 0; j < tam_provs; ++j) {
  29.             if (p[i].provs[j].pib <= x) ++sum_provs_less_x;
  30.             if (sum_provs_less_x == 2) j = tam_provs;
  31.         }
  32.         if (sum_provs_less_x >= 2) {
  33.             for (int j = 0; j < tam_provs; ++j) {
  34.                 suma_hab += p[i].provs[j].habitants;
  35.             }
  36.         }
  37.     }
  38.     return suma_hab;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment