• 出现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中,出现某位数字的次数的更多相关文章

  1. 数字序列中某一位数字(《剑指offer》面试题44)

    由于这道题目在牛客上没有,所以在此记录一下. 一.题目大意: 数字以0123456789101112131415…的格式序列化到一个字符序列中.在这个序列中,第5位(从0开始计数,即从第0位开始)是5 ...

  2. 计算1到n整数中,字符ch出现的次数

    个位ch个数 + 十位ch个数 * 10 + 百位ch个数 * 100:同时如果某一位刚好等于ch,还需要减去多算的一部分值. #include <stdio.h> //整数1到n,字符c ...

  3. Java基础知识强化69:基本类型包装类之Character案例(统计字符串中大写小写以及数字的次数)

    我们直接看案例如下: package cn.itcast_03; import java.util.Scanner; /* * 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数.(不考虑 ...

  4. 用VBA计算WPS 表格ET EXCEL中的行数和列数的多重方法

    用VBA计算WPS 表格ET EXCEL中的行数和列数 每种方法中上面的是Excel的行数,下面的是Excel的列数. 方法1: ActiveSheet.UsedRange.Rows.Count Ac ...

  5. 12月16日 增加一个购物车内product数量的功能, 自定义method,在helper中定义,计算代码Refactor到Model中。

    仿照Rails实战:购物网站 教材:5-6 step5:计算总价,做出在nav上显示购物车内product的数量. 遇到的❌: 1. <% sum = 0 %> <% current ...

  6. 071——VUE中vuex之使用getters计算每一件购物车中商品的总价

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 070——VUE中vuex之使用getters计算每一件购物车中商品的总价

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. sqlserver中计算某个特殊字符在字符串中出现的位置

    -- ============================================= -- Author: Evan -- Create date: 2018年3月15日10:: -- D ...

  9. 【Transact-SQL】计算整个表中所有值的出现的次数

    原文:[Transact-SQL]计算整个表中所有值的出现的次数 一个表有3列,5行,那么一共有15个值,现在要计算整个表中所有值在表中出现的次数,不过这里表的列数是不确定的,上面的例子是3列,实际上 ...

随机推荐

  1. python操作excel----openpyxl模块

    openpyxl模块支持.xls和.xlsx格式的excel创建,但是只支持.xlsx格式的读取操作,不支持.xls的读取(可以使用xlrd模块来读取,写入操作也可使用xlwt模块),也可使用pand ...

  2. C# winform DataGridView 绑定数据的的几种方法

    1.用DataSet和DataTable为DataGridView提供数据源 String strConn = "Data Source=.;Initial Catalog=His;User ...

  3. mysql运维入门6:MySQL读写分离

    Amoeba 以MySQL为底层数据存储,并对应用提供MySQL协议接口的proxy 集中想用应用的请求,根据用户事先设置的规则,将SQL请求发送到特定的数据库上执行 基于此可以实现负载均衡.读写分离 ...

  4. javascript 内存和连等赋值

    JavaScript深入之内存空间详细图解 https://juejin.im/post/5bf4c3eff265da613356348a 理解javascript中的连续赋值 https://www ...

  5. LightOJ1236

    题目大意: 给你一个 n,请你找出共有多少对(i,j)满足 lcm(i,j) = n (i<=j) . 解题思路: 我们利用算术基本定理将 n,i,j 进行分解: n = P1a1 * P2a2 ...

  6. 统计元音(hdu20)

    输入格式:输入一个整型,再循环输入带空格的字符串. 思考:先用scanf()函数输入一个整型,后面直接来个大循环,带空格字符串输入直接用gets()函数. 注意:由于scanf()里面多加了%c,&a ...

  7. List的扩容机制,你真的明白吗?

    一:背景 1. 讲故事 在前一篇大内存排查中,我们看到了Dictionary正在做扩容操作,当时这个字典的count=251w,你把字典玩的66飞起,其实都是底层为你负重前行,比如其中的扩容机制,当你 ...

  8. oracle计算两日期相差多少秒,分钟,小时,天,周,月,年

    --计算两个时间差相差多少秒select ceil((sysdate-t.transdate)* 24 * 60 * 60),t.transdate,sysdate from esc_trans_lo ...

  9. python时间戳和时间字符串的转换

    # -*- coding: utf-8 -*-# date=2020/3/27import timeimport uuid def getTimestamp_1770(): now_1770 = ro ...

  10. SpringBoot—自定义线程池及并发定时任务模板

    介绍   在项目开发中,经常遇到定时任务,今天通过自定义多线程池总结一下SpringBoot默认实现的定时任务机制. 定时任务模板 pom依赖 <dependencies> <dep ...