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. core.async中go的作用研究

    (defmacro go "Asynchronously executes the body, returning immediately to the calling thread. Ad ...

  2. 【HDU4706】Children's Day

    http://acm.hdu.edu.cn/showproblem.php?pid=4706 水题,也不知道有没有spj // <4706.cpp> - 11/03/16 14:11:21 ...

  3. eclipse faild to creat the java Virtual Machine的解决办法

    打开eclipse的时候突然出现了 faild to creat the java Virtual Machine 解决办法:打开解压后的Eclipse文件夹,找到eclipse.ini配置文件 打开 ...

  4. codeforces 37 E. Trial for Chief【spfa】

    想象成一层一层的染,所以相邻的两个格子连边,边权同色为0异色为1,然后答案就是某个格子到距离它最远得黑格子的最短距离的最小值 注意特判掉不需要染色的情况 #include<iostream> ...

  5. justify-content属性

    justify-content 用于设置或检索弹性盒子元素在主轴方向上的对齐方式. 属性值:flex-start 属性值:flex-end 属性值:center 属性值:space-between 属 ...

  6. 洛谷 P3372 【模板】线段树 加法

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个 ...

  7. [TJOI2012]桥

    Description 有n个岛屿,m座桥,每座桥连通两座岛屿,桥上会有一些敌人,玩家只有消灭了桥上的敌人才能通过,与此同时桥上的敌人会对玩家造成一定伤害.而且会有一个大Boss镇守一座桥,以玩家目前 ...

  8. C#将类对象转换为字典

    主要是实现将类里面 的属性和对应的值转换为字典的键和值. public class RDfsedfw { /// <summary> /// 将匿名类转换为字典 /// </summ ...

  9. Hadoop Hive概念学习系列之hive的正则表达式初步(六)

    说在前面的话 hive的正则表达式,是非常重要!作为大数据开发人员,用好hive,正则表达式,是必须品! Hive中的正则表达式还是很强大的.数据工作者平时也离不开正则表达式.对此,特意做了个hive ...

  10. Git学习笔记(0)-错误汇总

    一.LF will be replaced by CRLF 1.发现问题 $ git add welcome.txt warning: LF will be replaced by CRLF in w ...