• 出现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. Codeforces1183A(A题)Nearest Interesting Number

    Polycarp knows that if the sum of the digits of a number is divisible by 3, then the number itself i ...

  2. 【遗传编程/基因规划】Genetic Programming

    目录 背景介绍 程序表示 初始化 (Initialization) Depth定义 Grow方法 Full方法 Ramped half-and-half方法 适应度(Fitness)与选择(Selec ...

  3. Hystrix入门

    hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与hystrix本身的功能不谋而合,因此Netflix团队将该框架命名为Hystrix,并使用 ...

  4. Pyqt5_QComboBox

    QComboBox 是一个集按钮和下拉选项于一体的控件,也称做下拉列表框 方法 addItem() 添加一个下拉选项 addItems() 从列表中添加下拉选项 Clear() 删除下拉选项集合中的所 ...

  5. NPM的由来——为什么要使用NPM

    原文:https://blog.csdn.net/qq_37696120/article/details/80507178 https://blog.csdn.net/hong10086/articl ...

  6. mysql 赋权语句

    grant all privileges on phplampDB.* to phplamp@localhost identified by '1234';

  7. PLC可编程控制器的结构和工作原理

    PLC的可编程控制器由的功能结构由cpu中央处理器,存储器和输入输出借口三部分组成 CPU Cpu的功能是完成plc所有的的控制和监视, Cpu中央处理去由控制器,寄存器,运算器.通过数据总线,地址总 ...

  8. 【半译】扩展shutdown超时设置以保证IHostedService正常关闭

    我最近发现一个问题,当应用程序关闭时,我们的应用程序没有正确执行在IHostedService中的StopAsync方法.经过反复验证发现,这是由于某些服务对关闭信号做出响应所需的时间太长导致的.在这 ...

  9. linux添加策略路由python脚本(待完善)

    #! _*_ coding:utf-8 _*_ import os,sys,re,fileinput,socket device_list = [] ip_list = [] ip_end = [] ...

  10. 剑指Offer之链表中倒数第k个结点

    题目描述 输入一个链表,输出该链表中倒数第k个结点.   思路:首先计算出链表的长度,再计算出倒数第k个是正数第几个,找到该结点即可. public ListNode FindKthToTail(Li ...