51nod - 1009 数字1的数量

给定一个十进制正整数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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值