给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个数。
例如:n = 12,包含了5个1。1,10,12共包含3个1,11包含2个1,总共5个1。
Input
输入N(1 <= N <= 10^9)
Output
输出包含1的个数
Input示例
12
Output示例
5
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
int dp[10][10];
int a[10];
int sum(int pos)
{
if (pos < 0)
{
return 1;
}
int result = 0;
for (int i = pos; i >= 0; i--)
{
result = result * 10 + a[i];
}
result++;
return result;
}
int dfs(int pos, int state, bool limit)
{
if (pos < 0)
{
if (state == 1)
{
return 1;
}
return 0;
}
if (!limit && dp[pos][state] != -1)
{
return dp[pos][state];
}
int up = limit? a[pos] : 9;
int result = 0;
if (state == 1)
{
if (limit)
{
result += sum(pos);
}
else
{
result += pow(10, pos + 1);
}
}
for (int i = 0; i <= up; i++)
{
result += dfs(pos - 1, i, limit && i == a[pos]);
}
if (!limit)
{
dp[pos][state] = result;
}
return result;
}
int solve(int n)
{
int pos = 0;
while (n > 0)
{
a[pos] = n % 10;
n /= 10;
pos++;
}
return dfs(pos - 1, 0, true);
}
int main()
{
int N;
cin >> N;
memset(dp, -1, sizeof(dp));
cout << solve(N) << endl;
return 0;
}
方法2:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
ll dp[10][10], a[10];
ll dfs(int pos, int num, int limit)
{
if (pos == -1)
{
return num;
}
if (!limit && dp[pos][num] != -1)
{
return dp[pos][num];
}
int up = limit ? a[pos] : 9;
ll result = 0;
for (int i = 0; i <= up; i++)
{
result += dfs(pos-1, num+(i==1), limit && i == a[pos]);
}
if (!limit)
{
dp[pos][num] = result;
}
return result;
}
ll solve(ll x)
{
int pos = 0;
while (x)
{
a[pos++] = x % 10;
x /= 10;
}
return dfs(pos - 1, 0, 1);
}
int main(void)
{
ll n;
memset(dp, -1, sizeof(dp));
cin >> n;
cout << solve(n) << endl;
return 0;
}