Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <vector>
- using namespace std;
- constexpr int N = 1000000000;
- /* Returns true if n is an unlucky number. */
- bool check(int n) {
- int sum, last, cur;
- bool has;
- sum = 0;
- last = -1;
- has = false;
- while (n > 0) {
- cur = n % 10; // Current digit.
- n /= 10;
- if (cur == 1 and last == 3) { // Checking if has 13 as a substring.
- has = true;
- }
- sum += cur; // Updating sum.
- last = cur; // Updating last digit.
- }
- return has and sum == 13; // Valid if has 13 as a substring and sum is 13.
- }
- int main() {
- vector<int> ans;
- // Generating all the unlucky numbers.
- for (int n = 1; n <= N; n++) {
- if (check(n)) {
- ans.push_back(n);
- }
- }
- // Printing all the unlucky numbers in ascending order as a vector declaration.
- printf("vector<int> ans = {");
- for (int i = 0; i < ans.size() - 1; i++) {
- printf("%d, ", ans[i]);
- }
- printf("%d};\n", ans.back());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement