Advertisement
Hippskill

Untitled

Jan 27th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include<stdio.h>
  3. #include<iostream>
  4. #include<vector>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<map>
  8. #include<set>
  9. #include<sstream>
  10. #include<cstring>
  11. #include<numeric>
  12. #include<limits.h>
  13. using namespace std;
  14.  
  15.  
  16. set<int> to;
  17. int lenAns = 0;
  18.  
  19.  
  20. bool found(string cur) {
  21.     bool f = false;
  22.     for (int i = 0; i <= 9999; i++) {
  23.         string c = "";
  24.         int cs = i;
  25.         while (cs > 0) {
  26.             cs += cs % 10 + '0';
  27.             cs /= 10;
  28.         }
  29.         if (c == "") {
  30.             c += '0';
  31.         }
  32.         if (cur.find(c)) {
  33.             if (to.count(i) != 0) {
  34.                 to.erase(i);
  35.                 f = true;
  36.             }
  37.         }
  38.     }
  39.     return f;
  40. }
  41.  
  42. void go(string cur, int len) {
  43.     if (to.size() == 0) {
  44.         if (len < lenAns) {
  45.             lenAns = len;
  46.             printf("%s\n", cur);
  47.         }
  48.         return;
  49.     }
  50.     bool any = false;
  51.     for (int i = 0; i < 10; i++) {
  52.         if (found(cur)) {
  53.             cur += ('0' + i);
  54.             go(cur, len + 1);
  55.             cur = cur.substr(0, cur.length() - 1);
  56.         }
  57.     }
  58. }
  59.  
  60. int main() {
  61. #ifndef ONLINE_JUDGE
  62.     freopen("input.txt", "r", stdin);
  63. #endif 
  64.     freopen("output.txt", "w", stdout);
  65.  
  66.  
  67.     for (int i = 0; i < 10; i++) {
  68.         for (int i = 0; i <= 9999; i++) {
  69.             to.insert(i);
  70.         }
  71.         to.erase(i);
  72.         string s = "";
  73.         s += i + '0';
  74.         go(s, 1);
  75.     }
  76.  
  77.  
  78.  
  79.  
  80.    
  81.  
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement