Advertisement
Geometrian

Number Puzzle

Sep 8th, 2015
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. //http://math.stackexchange.com/questions/1427250/number-of-solutions-in-this-number-puzzle
  2. #include <cstdio>
  3. #include <cstdint>
  4. #include <cinttypes>
  5. inline bool okay(int64_t num) {
  6.     int counts[10] = {0,0,0,0,0,0,0,0,0,0};
  7.     int64_t temp;
  8.  
  9.     temp = num;
  10.     for (int i=0;i<10;++i) {
  11.         int digit = temp%10;
  12.         ++counts[digit];
  13.         temp /= 10;
  14.     }
  15.  
  16.     temp = 0;
  17.     for (int i=0;i<10;++i) {
  18.         temp = temp*10 + counts[i];
  19.     }
  20.  
  21.     return temp == num;
  22. }
  23. int main(int /*argc*/, char* /*argv*/[]) {
  24.     #if 0
  25.         printf("%d\n",okay(6210001000));
  26.         getchar();
  27.     #else
  28.         int64_t num = 0LL;
  29.         int64_t maximum = 9999999999LL;
  30.         while (num <= maximum) {
  31.             if (okay(num)) printf("%" PRId64 "\n",num);
  32.             ++num;
  33.             if (num%10000000LL==0) {
  34.                 printf("%.3f%% complete\n",100.0*static_cast<double>(num)/static_cast<double>(maximum));
  35.             }
  36.         }
  37.         getchar();
  38.     #endif
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement