V
来源:互联网 发布:傲世三国群雄争霸java 编辑:程序博客网 时间:2024/04/26 10:13
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 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).
For each case, print the case number and the number of zeroes written down by Jimmy.
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
让求的是【l,r】内数中含有的0的个数。直接数位dp,加上判断就可以了。代码如下:
#include<iostream>#include<cstring>using namespace std;typedef long long LL;int bit[20];LL dp[20][20];// 数位 0的个数 前导0 上界LL dfs(int pos, int sta, bool lead, bool limit) {if (pos == -1) {if (lead)return 1;return sta;} if (!lead && !limit && dp[pos][sta] != -1)return dp[pos][sta];int up = limit ? bit[pos] : 9;LL temp = 0;for (int i = 0; i <= up; i++){if (lead == 1)temp += dfs(pos - 1, 0, i == 0, limit && i == up);//当前还是不是0else{if (i == 0)temp += dfs(pos - 1, sta + 1, 0, limit && i == up);//非前导零的0要把0数加1elsetemp += dfs(pos - 1, sta, 0, limit && i == up);}}if (!limit && !lead)dp[pos][sta] = temp;return temp;}LL solve(LL x){int pos = 0;while (x){bit[pos++] = x % 10;x /= 10;}return dfs(pos - 1, 0, 1, 1);}int main(){int t;LL le, ri;cin >> t;int cnt = 1;while (t--){memset(dp, -1, sizeof(dp));cin >> le >> ri;cout << "Case " << cnt++ << ": ";cout << solve(ri) - solve(le - 1) << endl;}return 0;}
阅读全文
0 0
- V
- v
- v
- v
- v
- v
- V
- V
- V
- V
- V
- V
- v
- V
- V
- V
- v
- V
- Angular JIT vs AOT
- iOS中在APP内加入AppStore评分功能
- 刷LeetCode(6)——ZigZag Conversion
- k:UDP打洞原理
- shell中变量问题(记住)
- V
- 成为Java顶尖程序员 ,看这11本书就够了
- 关于Linux入门
- DML、DDL、DCL区别
- SPOJ Count on a tree II(树上莫队)
- img绝对路径相对路径练习练习
- Java Web 的No result defined for action cn.itcast.action.CustomerAction and result input 错误
- leetcode: 17. Letter Combinations of a Phone Number
- 从H264到RTP