计算从1到n中,出现某位数字的次数
- 出现1-9中某位数字次数的算法
/**
* @param input 整数n(1 ≤ n ≤ 1,000,000,000)
* @return 1-9中某个数字在数列中出现的次数
*/
public int calcCount(int input, int x) {
int count = 0;
int temp;
// 依次从个位往上开始计算
for (int i = 1; (temp = input / i) != 0; i *= 10) {
count += (temp / 10) * i;
int current = temp % 10;
if (current > x) {
// 还会出现i次
count += i;
} else if (current == x) {
// (input - temp * i)代表当前位置的低位数字
count += input - temp * i + 1;
}
}
Log.d(TAG, "calcCount() called with: input = [" + input + "], count = [" + count + "]");
return count;
}
- 出现数字0出现次数的算法
/**
* @param input 整数n(1 ≤ n ≤ 1,000,000,000)
* @return 0在数列中出现的次数
*/
public int calcZeroCount(int input) {
int count = 0;
int temp;
// 依次从个位往上开始计算,至最高位-1为止
for (int i = 1; (temp = input / i) / 10 != 0; i *= 10) {
count += (temp / 10) * i;
if (temp % 10 == 0) {
// (input - temp * i)代表当前位置的低位数字,去除首位为0的数字
count += input - temp * i + 1 - i;
}
}
return count;
}
- 出现0-9中某位数字次数的综合算法
public int count(int input, int x) {
int count = 0;
int temp;
// 依次从个位往上开始计算
for (int i = 1; (temp = input / i) != 0; i *= 10) {
// 高位数字
int high = temp / 10;
if (x == 0) {
if (high != 0) {
high--;
} else {
break;
}
}
count += high * i;
int current = temp % 10;
if (current > x) {
count += i;
} else if (current == x) {
// (input - temp * i)代表当前位置的低位数字
count += input - temp * i + 1;
}
}
Log.d(TAG, "count() called with: input = [" + input + "], count = [" + count + "]");
return count;
}
计算从1到n中,出现某位数字的次数的更多相关文章
- 数字序列中某一位数字(《剑指offer》面试题44)
由于这道题目在牛客上没有,所以在此记录一下. 一.题目大意: 数字以0123456789101112131415…的格式序列化到一个字符序列中.在这个序列中,第5位(从0开始计数,即从第0位开始)是5 ...
- 计算1到n整数中,字符ch出现的次数
个位ch个数 + 十位ch个数 * 10 + 百位ch个数 * 100:同时如果某一位刚好等于ch,还需要减去多算的一部分值. #include <stdio.h> //整数1到n,字符c ...
- Java基础知识强化69:基本类型包装类之Character案例(统计字符串中大写小写以及数字的次数)
我们直接看案例如下: package cn.itcast_03; import java.util.Scanner; /* * 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数.(不考虑 ...
- 用VBA计算WPS 表格ET EXCEL中的行数和列数的多重方法
用VBA计算WPS 表格ET EXCEL中的行数和列数 每种方法中上面的是Excel的行数,下面的是Excel的列数. 方法1: ActiveSheet.UsedRange.Rows.Count Ac ...
- 12月16日 增加一个购物车内product数量的功能, 自定义method,在helper中定义,计算代码Refactor到Model中。
仿照Rails实战:购物网站 教材:5-6 step5:计算总价,做出在nav上显示购物车内product的数量. 遇到的❌: 1. <% sum = 0 %> <% current ...
- 071——VUE中vuex之使用getters计算每一件购物车中商品的总价
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 070——VUE中vuex之使用getters计算每一件购物车中商品的总价
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- sqlserver中计算某个特殊字符在字符串中出现的位置
-- ============================================= -- Author: Evan -- Create date: 2018年3月15日10:: -- D ...
- 【Transact-SQL】计算整个表中所有值的出现的次数
原文:[Transact-SQL]计算整个表中所有值的出现的次数 一个表有3列,5行,那么一共有15个值,现在要计算整个表中所有值在表中出现的次数,不过这里表的列数是不确定的,上面的例子是3列,实际上 ...
随机推荐
- git推送代码问题之:ERROR: [abcdefg] missing Change-Id in commit message footer
一.问题: 在日常的工作中,使用git推送代码时会出现以下报错,“missing Change-Id in commit message” : qinjiaxi:$ git push origin H ...
- docker 部署jenkins
1.拉取镜像 docker pull jenkins/jenkins 2.运行jenkins镜像作为容器 运行命令如下: docker run -d -p 9086:8080 -p 50000:500 ...
- XCode Interface Builder开发——1
XCode Interface Builder开发--1 创建Xcode项目 选择第二个选项 选择Single View App,点击Next 设置完后点击Next Xcode基本面板 导航面板 工具 ...
- 判断iptables是否运行的一些探索
现在有这么一个需求,要判断iptables是否开启.咋看比较难以入手,那么可以想,怎么启动iptables,redhat下很容易联想到/etc/init.d/iptabes start . 我们可以来 ...
- 你 MySQL 中重复数据多吗,教你一招优雅的处理掉它们!
在需要保证数据唯一性的场景中,个人觉得任何使用程序逻辑的重复校验都是不可靠的,这时只能在数据存储层做唯一性校验.MySQL 中以唯一键保证数据的唯一性,那么若新插入重复数据时,我们可以让 MySQL ...
- CukeTest+Puppeteer的Web自动化测试(一)
CukeTest+Puppeteer的Web自动化测试 一.初识BDD.Cucumber(黄瓜).CukeTest 行为驱动开发(Behavior Driven Development,BDD).行为 ...
- 【MySQL】深入理解MySQL锁和事务隔离级别
先看个小案例: 话不多说,上案例,先创建一个表 mysql> CREATE TABLE IF NOT EXISTS `account`( `id` INT UNSIGNED AUTO_INCRE ...
- CSS3中的rem单位
一.rem介绍 rem是什么? 它的全称是 font size of the root element (根元素的字体大小) 它是CSS3中新增加的一个尺寸(度量)单位,根节点(html)的font- ...
- Verilog代码和FPGA硬件的映射关系(二)
大家可能会有这样的疑问,我们编写的Verilog代码最终会在FPGA上以怎样的映射关系来实现功能呢?我们以一个最简单的组合逻辑与门为例来向大家说明.RTL代码如下所示: //------------- ...
- MongoDB -MSC集群的部署
一.Shard节点配置过程 1. 目录创建:mkdir -p /mongodb/38021/conf /mongodb/38021/log /mongodb/38021/datamkdir -p ...