从所有一位数包含的1开始向上递推所有k位数包含的1,递推式:
ak = ak-1 * 10 + pow(10,k-1);
AC代码:
#include <vector> #include <cstdio> using namespace std; int main(){ int n; vector<int> rec; scanf("%d",&n); int nc(n); while(n != 0){ rec.push_back(n % 10); n /= 10; } vector<int> r(rec.size()); vector<int> rem; int radix(1); for(int i = 0;i < r.size();i++){ rem.push_back(radix); if(i == 0) r[0] = 1; else{ r[i] = r[i - 1] * 10 + radix; } radix *= 10; } int ret(0); for(int i = rec.size() - 1;i >= 0;i--){ if(i != 0 && rec[i] != 0){ if(rec[i] == 1) ret += r[i - 1] + nc % rem[i] + 1; else ret += r[i - 1] * rec[i] + rem[i]; } else if(i == 0 && rec[i] != 0) ret += 1; } printf("%d\n",ret); return 0; }