计算从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列,实际上 ...
随机推荐
- @vue/cli 4.0.5 学习记录
1. Vue CLI (@vue/cli) 是一个全局安装的 npm 包,提供了终端里的 vue 命令.Vue CLI 插件的名字以 @vue/cli-plugin- (内建插件) 或 vue-cli ...
- P1251 餐巾计划问题 网络流
P1251 餐巾计划问题 #include <bits/stdc++.h> using namespace std; typedef long long ll; , inf = 0x3f3 ...
- base64编码的字符串(含有中文) 前端解码
base64编码的字符串(含有中文) 前端解码 https://xue5602.github.io/2018/12/19/atob%E8%A7%A3%E7%A0%81utf-8%E5%AD%97%E7 ...
- java第十二周课后作业0523
1.编写一个程序,实现字符串大小写的转换并倒序输出.要求如下(1)使用for循环将字符串“ Hello world”从最后一个字符开始遍历(2)遍历的当前字符如果是大写字符,就使用 toLower C ...
- Spring Boot 教程 (4) - swagger-ui
Spring Boot 教程 - swagger-ui 1. 什么是Swagger? Swagger™的目标是为REST APIs 定义一个标准的,与语言无关的接口,使人和计算机在看不到源码或者看不到 ...
- Cube-UI组件中create-api 模块的基本使用
1.这个模块的功能是什么? 官方文档是这样解释的: 该模块默认暴露出一个 createAPI 函数,可以实现以 API 的形式调用自定义组件.并且既可以在 Vue 实例上下文中调用,也可以在普通 js ...
- mysql创建本地用户及赋予数据库权限的方法示例
大家在安装 mysql 时通常会生成一个超级用户 root,很多人之后就一直沿用这一个用户,虽然这会很方便,但超级用户权限太大,在所有地方使用它通常是一个安全隐患. 这一点跟操作系统的用户管理也是类似 ...
- tomcat依赖--linux
https://tomcat.apache.org/download-90.cgi 下载 3,解压到usr/locat/tomcat 创建usr/local/tomcat Mkdir /usr/loc ...
- Collection接口和list,set子类
Collection接口常用的子接口有:List接口.Set接口List接口常用的子类有:ArrayList类.LinkedList类Set接口常用的子类有:HashSet类.LinkedHashSe ...
- 使用webstorm 搭建 vue 开发环境
一.首先安装 node.js 安装成功后在cmd里面使用:node -v 命令查看安装是否成功 下载链接: 链接:https://pan.baidu.com/s/1CL9J4Ryp3sL0zPYKJy ...