vlatkovski

Potoci

Aug 22nd, 2016
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int potok(const int&);
  8. int pobaraj(vector<int>& v1, vector<int>& v2);
  9.  
  10. int main() {
  11.     int n;
  12.     cin >> n;
  13.  
  14.     int a = 1, b = 3, c = 9;
  15.  
  16.     vector<int> vn = {n};
  17.     vector<int> va = {a};
  18.     vector<int> vb = {b};
  19.     vector<int> vc = {c};
  20.  
  21.     int najdeno = -1, najdenoi = -1;
  22.     int x = 0;
  23.  
  24.     while (najdeno == -1) {
  25.         x = pobaraj(vn, va);
  26.         if (x) {
  27.             najdeno = 1;
  28.             najdenoi = x;
  29.             break;
  30.         }
  31.         x = pobaraj(vn, vb);
  32.         if (x) {
  33.             najdeno = 3;
  34.             najdenoi = x;
  35.             break;
  36.         }
  37.         x = pobaraj(vn, vc);
  38.         if (x) {
  39.             najdeno = 9;
  40.             najdenoi = x;
  41.             break;
  42.         }
  43.  
  44.         n = potok(n);
  45.         vn.push_back(n);
  46.  
  47.         a = potok(a);
  48.         va.push_back(a);
  49.  
  50.         b = potok(b);
  51.         vb.push_back(b);
  52.  
  53.         c = potok(c);
  54.         vc.push_back(c);
  55.     }
  56.  
  57.     cout << najdeno << ' ' << najdenoi << endl;
  58.  
  59.     return 0;
  60. }
  61.  
  62. int potok(const int& x) {
  63.     int sumaCifri = 0;
  64.  
  65.     int x1 = x;
  66.     while (x1 != 0) {
  67.         sumaCifri += x1 % 10;
  68.         x1 /= 10;
  69.     }
  70.  
  71.     return x + sumaCifri;
  72. }
  73.  
  74. int pobaraj(vector<int>& v1, vector<int>& v2) {
  75.     static vector<int> v_intersection;
  76.     set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v_intersection));
  77.  
  78.     if (v_intersection.empty()) {
  79.         return 0;
  80.     } else {
  81.         return v_intersection[0];
  82.     }
  83. };
Advertisement
Add Comment
Please, Sign In to add comment