1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <iostream>
#include <cctype>
#include <iterator>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
#include <cmath>
int main()
{
enum errs { OK = 0, DUPDIG, TOOFEW, TOOMANY, NOTNUM, BADNUM, NOTODD, BADTENS };
const char* errmsg[] {"OK!", "Duplicate digit", "Too few digits", "Too many digits", "Not a number", "Incorrect digit sum", "Not an odd number", "Tens not 3 times thousands"};
constexpr size_t total {27};
constexpr size_t startnum {1035};
constexpr size_t maxdigits {9};
static_assert (BADTENS == std::size(errmsg) - 1);
for (size_t nodigits = 4; nodigits <= maxdigits; ++nodigits) {
size_t cnt {};
for (size_t n = startnum, maxnum = std::pow(10, nodigits) - 1; n < maxnum; n += 2) {
std::string sno {std::to_string(n)};
std::stringstream iss;
iss << std::setw(nodigits) << std::setfill('0') << sno << '\n';
iss.seekg(0);
char ch {};
std::vector<int> digits(nodigits);
size_t sum {}, nod {};
int err {OK};
for (bool dup[10] {}; !err && (ch = iss.get()) != '\n' && nod < nodigits && std::isdigit(ch); dup[digits[nod++]] = true) {
sum += (digits[nod] = ch - '0');
err = dup[digits[nod]] == true;
}
//err += (!err && ch == '\n' && nod < nodigits) * TOOFEW;
//err += (!err && std::isdigit(ch) && nod == nodigits) * TOOMANY;
//err += (!err && (ch != '\n' || nod != nodigits)) * NOTNUM;
err += (!err && sum != total) * BADNUM;
//err += (!err && digits[nodigits - 1] % 2 == 0) * NOTODD;
err += (!err && digits[nodigits - 4] != 3 * digits[nodigits - 2]) * BADTENS;
if (err == 0) {
//std::cout << std::setw(nodigits) << std::setfill('0') << n << '\n';
++cnt;
}
}
std::cout << nodigits << " digits have " << cnt << " number(s)\n";
}
}
|