[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 ...
随机推荐
- Binary safe
https://en.wikipedia.org/wiki/Binary-safe A binary-safe function is one that treats its input as a r ...
- sql加一个%号是什么意思
sql%notfound 是异常SQL%ROWCOUNT SQL语句执行影响的行数SQL%FOUND SQL语句是否成功执行SQL%NOTFOUND SQL语句是否成功执行SQL%ISOPEN 游标是 ...
- ORACLE分区表发挥性能
1.1 分区表PARTITION table 在ORACLE里如果遇到特别大的表,可以使用分区的表来改变其应用程序的性能. 1.1.1 分区表的建立: 某公司的每年产生巨大的销售记录,DBA向公司建议 ...
- Python不兼容问题
今天遇到了一个Python2与3不兼容的坑. ride是基于robot框架的python自动化ui,但它只支持python2,而我电脑环境只有python3,想跑别人基于ride编写的测试用例,折腾了 ...
- 使用AngularJS创建应用的5个框架(转)
原文地址:http://www.php100.com/html/dujia/2015/0206/8580.html 本文由PHP100中文网编译,转载请看文末的转载要求,谢谢合作! 如果你计划使用An ...
- 近年来火热的人工智能,其实是IT业界的一个障眼法
近年来火热的人工智能,其实是IT业界的一个障眼法,仗着现在的计算机的计算能力牛B,把一个类仿生统计算法,宣传成了人工智能,不得不感叹一些营销人士的牛逼,说大话不腰疼.当然谎言重复一千遍也许自己也就信了 ...
- 清北考前刷题day6早安
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...
- ECMA里面的操作符,
ECMA里面的操作符,描述了一组操作于数据值的操作符,包括算数操作符.位操作符,关系操作符和相等操作符,ECMAscript操作符与之不同的是,他们能够使用于很多值,例如字符串.数字值.布尔值.甚至对 ...
- U - Relatives(欧拉函数)
Description Given n, a positive integer, how many positive integers less than n are relatively prime ...
- Windows Server 2008 R2关闭FTP服务
公司在ZJ的项目给了一台互联网可以访问的测试服务器,但是只给了三个访问端口,而且还做了映射. 映射信息如下:[1050->3389,1051->50000,1053->21] 其中1 ...