zoukankan      html  css  js  c++  java
  • 51nod 1009 数字1的数量 数位dp

    基准时间限制:1 秒 空间限制:131072 KB
     
    给定一个十进制正整数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<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    const int N=1e3+10,M=1e6+1010,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    int dp[N][N];
    char a[110];
    int main()
    {
        int x,y;
        int ten=1;
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=10;i++)
        {
            for(int t=0;t<=10;t++)
            if(t==1)
            dp[i][t]=dp[i-1][9]*2+ten;
            else
            dp[i][t]=dp[i-1][9]+dp[i][t-1];
            ten*=10;
        }
        while(~scanf("%s",&a))
        {
            int ans=0;
            int x=strlen(a);
            int flag=x;
            for(int i=0;i<flag;i++)
            {
                if(a[i]=='0'){
                        x--;
                        continue;
                }
                int num=0;
                for(int t=i+1;t<flag;t++)
                num=num*10+a[t]-'0';
                if(a[i]=='1')
                ans+=dp[x-1][9]+1+num;
                else
                ans+=dp[x][a[i]-'0'-1];
                x--;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    Python2 和 Python3的区别 更新中
    CentOS下图形界面安装_Orcaale 11g
    Nmap_使用介绍
    shell_innobackup增量备份步骤
    shell_跳板机推送公钥
    shell_clean_log
    shell_xtrabackup_backup_mysql
    gitlab免密登录
    gitlab安装与部署
    git合并分支
  • 原文地址:https://www.cnblogs.com/jhz033/p/5892192.html
Copyright © 2011-2022 走看看