Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to get the first 100 cyclops numbers (number with odd number of digits and zero in the center) in C++

1 Answer

0 votes
#include <vector>
#include <iomanip>
#include <iostream>

template <class T>
void printVector(std::vector<T> vec, size_t total_in_iine) {
    size_t col = 0;
    
    for (auto n : vec) {
        std::cout << std::setw(6) << n << " ";
        col++;
        if (col == total_in_iine) {
            std::cout << "\n";
            col = 0;
        }
    }
}

bool isCyclopsNumber(int n) {
    if (n == 0) {
        return true;
    }
    
    int m = n % 10;
    int count = 0;
    while (m != 0) {
        count++;
        n /= 10;
        m = n % 10;
    }
    
    n /= 10;
    m = n % 10;
    while (m != 0) {
        count--;
        n /= 10;
        m = n % 10;
    }
    
    return n == 0 && count == 0;
}

std::vector<int> getFirstCyclopsUpTo(int total) {
    std::vector<int> cyclops;
    
    int i = 0;
    while (cyclops.size() < total) {
        if (isCyclopsNumber(i)) {
            cyclops.push_back(i);
        }
        i++;
    }
    
    return cyclops;
}


int main(void) {
    auto first100cyclops = getFirstCyclopsUpTo(100);

    printVector<int>(first100cyclops, 10);
}





/*
run:

     0    101    102    103    104    105    106    107    108    109 
   201    202    203    204    205    206    207    208    209    301 
   302    303    304    305    306    307    308    309    401    402 
   403    404    405    406    407    408    409    501    502    503 
   504    505    506    507    508    509    601    602    603    604 
   605    606    607    608    609    701    702    703    704    705 
   706    707    708    709    801    802    803    804    805    806 
   807    808    809    901    902    903    904    905    906    907 
   908    909  11011  11012  11013  11014  11015  11016  11017  11018 
 11019  11021  11022  11023  11024  11025  11026  11027  11028  11029 
 
 */

 



answered Mar 28, 2023 by avibootz
edited Mar 28, 2023 by avibootz
...