祭りらしいですのつづき:
文字列に変換して'0'を勘定すんじゃなく、まぢめに各桁の0を勘定するます。
// Radix進数での'桁'
template<unsigned Radix>
class Digit {
private:
unsigned value_; // 値
Digit* next_; // 次の桁
public:
Digit() : value_(), next_(nullptr) { clear(); }
~Digit() { delete next_; }
// increment
void inc() {
// +1して桁あふれコいちまったら
if ( ++value_ == Radix ) {
value_ = 0; // 0に戻して
if ( !next_ ) next_ = new Digit(); // ひとつ上の桁を
next_->inc(); // +1する。
}
}
// n の個数を返す
unsigned count(unsigned n) const
{ return (value_ == n ? 1 : 0) + ( next_ ? next_->count(n) : 0 ); }
void clear() { value_ = 0; if ( next_ ) next_->clear(); }
};
#include <iostream>
int main() {
Digit<10> counter;
unsigned sum = 0;
for ( int i = 0; i < 1001; ++i ) {
sum += counter.count(0); // 0の個数を積算しつつ
counter.inc(); // +1をくりかえす
}
std::cout << sum << std::endl;
}