现在的位置: 首页 > 综合 > 正文

XDU 1160 – 科协的数字游戏I

2018年01月20日 ⁄ 综合 ⁄ 共 1182字 ⁄ 字号 评论关闭
Problem 1160 - 科协的数字游戏I
Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty
Total Submit: 184  Accepted: 32  Special Judge: No
Description

 科协里最近很流行数字游戏。某人命名了一种不降数,这种数字必须满足从左到右各位数字成大于等于的关系,如123,446。现在大家决定玩一个游戏,指定一个整数闭区间[a,b],问这个区间内有多少个不降数。

Input
题目有多组测试数据。每组只含2个数字a, b (1 <= a, b <= 2^31)。
Output
每行给出一个测试数据的答案,即[a, b]之间有多少阶梯数。
Sample Input
1 9 
1 19
Sample Output
9
18
Hint
Source

            tclh123 

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int a,b,dp[20][20],bit[20];

int dfs(int pos,int s,bool limit)
{
    if(pos==-1return 1;
    if(!limit&&~dp[pos][s]) return dp[pos][s];
    int end=limit?bit[pos]:9;
    int ans=0;
    for(int i=s;i<=end;i++)
        ans+=dfs(pos-1,i,limit&&(i==end));
    if(!limit)
        dp[pos][s]=ans;
    return ans;
}

int main()
{
    memset(dp,-1,sizeof(dp));
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        int len=0;
        a--;
        while(a)
        {
            bit[len++]=a%10;
            a/=10;
        }
        int A=dfs(len-1,0,true);
        len=0;
        while(b)
        {
            bit[len++]=b%10;
            b/=10;
        }
        int B=dfs(len-1,0,true);
        printf("%d\n",B-A);
    }

    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Manni )

抱歉!评论已关闭.