classCount2 { public: intcountNumberOf2s(int n) { int flag = 1; int high = 0; int low = 0; int cur = 0; int res = 0; while (n / flag) { low = n - (n / flag)*flag; cur = (n / flag) % 10; high = n / (flag * 10); if (cur < 2) { res += flag*high; } elseif (cur == 2) { res += flag*high + low + 1; } elseif (cur > 2) { res += flag*(high + 1); } flag *= 10; } return res; } };