Advertisement
VictorXjoeY

Naive unlucky numbers generator

Nov 15th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. constexpr int N = 1000000000;
  7.  
  8. /* Returns true if n is an unlucky number. */
  9. bool check(int n) {
  10.     int sum, last, cur;
  11.     bool has;
  12.  
  13.     sum = 0;
  14.     last = -1;
  15.     has = false;
  16.  
  17.     while (n > 0) {
  18.         cur = n % 10; // Current digit.
  19.         n /= 10;
  20.  
  21.         if (cur == 1 and last == 3) { // Checking if has 13 as a substring.
  22.             has = true;
  23.         }
  24.  
  25.         sum += cur; // Updating sum.
  26.         last = cur; // Updating last digit.
  27.     }
  28.  
  29.     return has and sum == 13; // Valid if has 13 as a substring and sum is 13.
  30. }
  31.  
  32. int main() {
  33.     vector<int> ans;
  34.  
  35.     // Generating all the unlucky numbers.
  36.     for (int n = 1; n <= N; n++) {
  37.         if (check(n)) {
  38.             ans.push_back(n);
  39.         }
  40.     }
  41.  
  42.     // Printing all the unlucky numbers in ascending order as a vector declaration.
  43.     printf("vector<int> ans = {");
  44.  
  45.     for (int i = 0; i < ans.size() - 1; i++) {
  46.         printf("%d, ", ans[i]);
  47.     }
  48.  
  49.     printf("%d};\n", ans.back());
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement