Description

Count the number of k's between 0 and nk 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的更多相关文章

  1. 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, ...

  2. LintCode "Digit Counts" !!

    Lesson learnt: one effective solution for bit\digit counting problems: counting by digit\bit http:// ...

  3. 欧拉工程第63题:Powerful digit counts

    题目链接 The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=8 ...

  4. 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 ...

  5. 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, ...

  6. 【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 ...

  7. Project Euler 63: Powerful digit counts

    五位数\(16807=7^5\)也是一个五次幂,同样的,九位数\(134217728=8^9\)也是一个九次幂.求有多少个\(n\)位正整数同时也是\(n\)次幂? 分析:设题目要求的幂的底为\(n\ ...

  8. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  9. 剑指offer ------ 刷题总结

    面试题3 -- 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 1. 每行中的整数从左到右是排序的. 2. 每行的第一个数大于上一行的最后一个整数. publi ...

随机推荐

  1. NTFS文件系统的单个文件最大到底有多大?

    于NTFS文件系统的单个文件最大到底有多大? 闲来无事突然想到这个问题,到网上搜索了一下也没有一个固定的解释. 于是到微软官方知识库去寻找答案: 注意:基础硬件限制可能会对任何文件系统施加额外的分区大 ...

  2. luogu1155 双栈排序

    题目大意 运用两个栈的push和pop操作使得一个序列单调递增且操作字典序最小.$n\leq 1000$. 题解 本题我们要尝试运用“瞪眼法”,也就是推样例.我们显然要数字尽可能地推入第一个栈.那么问 ...

  3. 容器LinkedList原理(学习)

    一.概述 数据结构和ArrayList有本质不同,LinkedList 是基于链表实现,它的插入和删除操作比 ArrayList 更加高效,基于链表的,所以随机访问的效率要比 ArrayList 差. ...

  4. ios23--动画做弹出提示框toast

    ) { /* [UIView animateWithDuration:2.0 animations:^{ // 执行动画 self.showHUB.text = @"当前购物车已空,赶紧买买 ...

  5. js验证手机号,身份证,车牌号验证

    js验证手机号  <input type="text" class="identificationno"> // 身份证号码为15位或者18位,15 ...

  6. 英式英语 vs 美式英语

    0. 常见不同 日期的表达: 美国:月日年: 英国:日月年: 1. 发音 schedule,美 ['skɛdʒul],英 [ˈʃɛdjuːl] pecan,山核桃,英 ['piːk(ə)n;],美 [ ...

  7. 洛谷 P1979 [ NOIP 2013 ] 华容道 —— bfs + 最短路

    题目:https://www.luogu.org/problemnew/show/P1979 真是一道好题... 首先考虑暴力做法,应该是设 f[i][j][x][y] 记录指定棋子和空格的位置,然后 ...

  8. POJ 2104 HDU 2665 主席树 解决区间第K大

    两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...

  9. 通过CSS控制页面中的内容垂直居中的方法

    方法一:通过行高(line-height)定位 line-height通常是用于调节一段文字的行与行之间的距离,或者说两行文字之间的距离,如果行高是500px,那么每一行中的文字距离本行的顶部就是25 ...

  10. C++中虚析构函数的作用 (转载)

    转自:http://blog.csdn.net/starlee/article/details/619827 我们知道,用C++开发的时候,用来做基类的类的析构函数一般都是虚函数.可是,为什么要这样做 ...