[Algorithm] 3. Digit Counts
Description
Count the number of k's between 0 and n. k can be 0 - 9.
Example
if n = 12
, k = 1
in
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
we have FIVE 1's (1, 10, 11, 12)
Answer
/**
* @param k: An integer
* @param n: An integer
* @return: An integer denote the count of digit k in 1..n
*/
int digitCounts(int k, int n) {
// Check every digit from 0 to n.
int count, temp, i = ;
for ( i=; i<=n; i++ )
{
temp = i;
do
{
// Check the ones and tens place respectively for the digits between 10 to 99.
if ( (temp >= ) && (temp < ) ) {
if ( (temp/) == k ) count++;
if ( (temp%) ==k ) count++;
} else { // Check the ones place only for other cases.
if ( (temp%) == k )
count++;
} temp /= ;
} while (temp >= );
} return count;
}
[Algorithm] 3. Digit Counts的更多相关文章
- Digit Counts
Count the number of k's between 0 and n. k can be 0 - 9. Example if n = 12, k = 1 in [0, 1, 2, 3, 4, ...
- LintCode "Digit Counts" !!
Lesson learnt: one effective solution for bit\digit counting problems: counting by digit\bit http:// ...
- 欧拉工程第63题:Powerful digit counts
题目链接 The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=8 ...
- Project Euler:Problem 63 Powerful digit counts
The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...
- 3. Digit Counts【medium】
Count the number of k's between 0 and n. k can be 0 - 9. Example if n = 12, k = 1 in [0, 1, 2, 3, ...
- 【Lintcode】003.Digit Counts
题目: Count the number of k's between 0 and n. k can be 0 - 9. Example if n = 12, k = 1 in [0, 1, 2, 3 ...
- Project Euler 63: Powerful digit counts
五位数\(16807=7^5\)也是一个五次幂,同样的,九位数\(134217728=8^9\)也是一个九次幂.求有多少个\(n\)位正整数同时也是\(n\)次幂? 分析:设题目要求的幂的底为\(n\ ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 剑指offer ------ 刷题总结
面试题3 -- 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 1. 每行中的整数从左到右是排序的. 2. 每行的第一个数大于上一行的最后一个整数. publi ...
随机推荐
- 训练深度学习网络时候,出现Nan是什么原因,怎么才能避免?——我自己是因为data有nan的坏数据,clear下解决
from:https://www.zhihu.com/question/49346370 Harick 梯度爆炸了吧. 我的解决办法一般以下几条:1.数据归一化(减均值,除方差,或者加入n ...
- [JSOI 2016] 最佳团体
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4753 [算法] 很明显的分数规划 可以用树形动态规划(树形背包)检验答案 时间复杂度 ...
- 第十二周 Leetcode 354. Russian Doll Envelopes(HARD) LIS问题
Leetcode354 暴力的方法是显而易见的 O(n^2)构造一个DAG找最长链即可. 也有办法优化到O(nlogn) 注意 信封的方向是不能转换的. 对第一维从小到大排序,第一维相同第二维从大到小 ...
- 杂项-Java:Ehcache
ylbtech-杂项-Java:Ehcache EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. 1.返回顶部 1. 基 ...
- POJ 2452 Sticks Problem (暴力或者rmq+二分)
题意:给你一组数a[n],求满足a[i] < a[k] < a[j] (i <= k <= j)的最大的 j - i . 析:在比赛时,我是暴力做的,虽然错了好多次,后来说理解 ...
- Builder Design pattern
string assemblyName = ConfigurationSettings["BuilderAssembly"]; string builderName = Confi ...
- vue 加载文件,省略后缀后的加载顺序
Vue使用import ... from ...来导入组件,库,变量等.而from后的来源可以是js,vue,json.这个是在webpack.base.conf.js中设置的: module.exp ...
- PKUACM 2018 D chocolate【并查集+克鲁斯卡尔】
传送:http://poj.openjudge.cn/practice/C18D/ 依然是课件截图 #include<iostream> #include<cstdio> #i ...
- C#命名空间 using的用法
using的用法: 1. using指令:引入命名空间 这是最常见的用法,例如: using System; using Namespace1.SubNameSpace; 2. using stati ...
- C. Coin Troubles 有依赖的背包 + 完全背包变形
http://codeforces.com/problemset/problem/283/C 一开始的时候,看着样例不懂,为什么5 * a1 + a3不行呢?也是17啊 原来是,题目要求硬币数目a3 ...