zoukankan      html  css  js  c++  java
  • lightoj 1140

    1140 - How Many Zeroes?

    Jimmy writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down?

    Input

    Input starts with an integer T (≤ 11000), denoting the number of test cases.

    Each case contains two unsigned 32-bit integers m and n, (m ≤ n).

    Output

    For each case, print the case number and the number of zeroes written down by Jimmy.

    Sample Input

    Output for Sample Input

    5

    10 11

    100 200

    0 500

    1234567890 2345678901

    0 4294967295

    Case 1: 1

    Case 2: 22

    Case 3: 92

    Case 4: 987654304

    Case 5: 3825876150

     代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #define N 1100
    #define mod 1000000007
    using namespace std;
    typedef long long ll;
    int num[N];
    ll dp[N][N];

    ll dfs(int pos, int status, int op, int limit)///
    {
    ///pos当前处理的位置(从高到底)。
    ///status要达到的状态,如果为1则可以认为找到了答案,到时候用来返回,给计数器+1。
    ///op判断前边是否出现非零的数,没为1, 否则为0.
    ///limit是否受限,也即当前处理这位能否随便取值。

    if(pos < 1)///已结搜到尽头,返回"是否找到了答案"这个状态。
    return op ? 1 : status;

    if(!limit && !op && dp[pos][status] != -1)///dp里保存的是完整的,也即不受限的答案,所以如果满足的话,可以直接返回。
    return dp[pos][status];

    int endd = limit ? num[pos] : 9;
    ll ans = 0;

    for(int i = 0; i <= endd; i++)
    {
    if(!op)///说明前便已经出现非零的数。
    {
    if(i == 0)///计数加1,status+1,op还为1.
    ans += dfs(pos-1, status + 1, 0, limit && i == endd);
    else
    ans += dfs(pos-1, status , 0, limit && i == endd);
    }

    else
    {
    if(i == 0)
    ans += dfs(pos-1, 0, 1, limit && i == endd);
    else
    ans += dfs(pos-1, 0, 1, limit && i == endd);
    }


    }
    if(!limit && !op)///这个地方要注意,在没有限制并且前边出现了非零的数就开始赋值。
    dp[pos][status] = ans;

    return ans;


    }

    ll cal(ll x)
    {
    int len = 0;

    while(x)
    {
    num[++len] = x % 10;
    x /= 10;
    }

    return dfs(len, 0, 1, 1);
    }
    int main(void)
    {
    int T , cas;
    ll n , m;
    scanf("%d", &T);

    cas = 0;
    memset(dp, -1, sizeof(dp));///这个一定要放在循环外边, 不然会TLE。
    while(T--)
    {
    cas++;
    scanf("%lld%lld", &n, &m);

    cal(m) - cal(n-1);
    printf("Case %d: %lld ", cas, cal(m) - cal(n-1));

    }
    return 0;
    }

  • 相关阅读:
    有关绑定没有数据显示的问题
    asp.net 打印控件之报表
    dockercompose安装,yml文件配置
    Docker日志文件切割以及大小配置
    查看Docker容器日志大小
    CSS控制 video 隐藏、显示进度条、播放按钮、全屏按钮
    根据url 下载文件
    自定义指令,按钮防连点
    iframe 内嵌页面以及传参
    C# 判断网站是否能访问或者断链
  • 原文地址:https://www.cnblogs.com/dll6/p/7206168.html
Copyright © 2011-2022 走看看