(算法)从0到n整数中数字2出现的次数
题目:
数出0到n(含)中数字2出现了几次。
思路:
1、暴力方法,数出每个数字包含几个2,然后累加起来。
2、分析:分别考虑数字n每一位出现2的次数,如123123;
从左往右考虑4123123;
考虑第一个1(即第6位),该位出现2的次数为4*10^6/10;
考虑第一个2(即第5位),该位出现2的次数为41*10^5/10+3123+1;
考虑第一个3(即第4位),该位出现2的次数为(412+1)*10^4/10;
附:除以10的原因在于:每10个数字,任意位出现2的概率为1/10.
总结规律:
第i位出现2个次数与该位所在的数字有关:
当第i位的数字小于2,出现次数就等于比其高位部分的数字*10^i/10,
当第i位的数字等于2,出现次数就等于比其高位部分的数字*10^i/10+n%(10^i),
当第i位的数字大于2,出现次数就等于(比其高位部分的数字+1)*10^i/10。
代码:
#include<iostream>
#include<sstream>
#include<math.h>
using namespace std; template<class out_T,class in_T>
out_T convert(const in_T &t){
stringstream ss;
out_T result;
ss<<t;
ss>>result;
return result;
} int count2sInRangeAtDigit(int number,int d){
int powerOf10=pow(,d);
int nextPowerOf10=powerOf10*;
int right=number%powerOf10; int roundDown=number-number%nextPowerOf10;
int roundUp=roundDown+nextPowerOf10; int digit=(number/powerOf10)%;
if(digit<)
return roundDown/;
else if(digit==)
return roundDown/+right+;
else
return roundUp/;
} int count2sInRange(int number){
int count=;
string str;
str=convert<string>(number);
int len=str.size(); for(int digit=;digit<len;digit++)
count+=count2sInRangeAtDigit(number,digit);
return count;
} int main(){
int n;
while(cin>>n){
cout<<count2sInRange(n)<<endl;
}
return ;
}
(算法)从0到n整数中数字2出现的次数的更多相关文章
- 233. Number of Digit One *HARD* -- 从1到n的整数中数字1出现的次数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- 输入一个正整数n,计算出[0,n]这些整数中的二进制数没有连续3个1的数字有多少
输入一个正整数n,计算出[0,n]这些整数中的二进制数没有连续3个1的数字有多少? 例子:输入数字9,则输出结果位9.因为[0-9]中,只有数字7有连续的三个‘1’出现,别的都没有,所以一共有9个数字 ...
- 计算1至n中数字X出现的次数
描述 计算 1 至 n 中数字 X 出现的次数,其中 $n \ge 1,X \in [0,9]$. 解题思路 这是一道比较简单的题目,举个例子先:假设 $n=11, X=1$,那么就是求 1, 2, ...
- 1到n的整数中,1出现的次数
参考链接:https://discuss.leetcode.com/topic/18054/4-lines-o-log-n-c-java-python 1到n的整数中,1出现的次数,如11中,1出现了 ...
- 编程算法 - 从1到n整数中1出现的次数 代码(C)
从1到n整数中1出现的次数 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入一个整数n, 求从1到n这n个整数的十进制表示中1出现的次数. ...
- 计算1至n中数字X出现的次数【math】
转自: nailperry 一.1的数目 编程之美上给出的规律: 1. 如果第i位(自右至左,从1开始标号)上的数字为0,则第i位可能出现1的次数由更高位决定(若没有高位,视高位为0),等于更高位数字 ...
- 冒泡算法给0~9随机n位数字排序
<?php //产生5位0~9的随机数 function getRand($begin=0,$end=9,$limit=5){ $rand_array=r ...
- 求一个区间[a,b]中数字1出现的次数
问题来源:http://ac.jobdu.com/problem.php?pid=1373 举例:如果n=10 那么1-10之间的1的个数是2(1,2,3,4,...10) 这其中有一个规律: 挨着看 ...
- 1~n中数字0~9出现的次数
题意:rt 分析: 当然不可能去遍历,应该寻找统计的方法. 如计算 78501 中 "5" 出现的次数. 我们可以枚举“5”出现的位置, 如当“5”位于倒数第2位时,写成 xxx5 ...
随机推荐
- GetKeyState(vk_control)
GetKeyState(vk_control) 返回负数 , 说明按键被按下了
- SUSE Linux – Zypper 命令示例
來源:http://www.linuxidc.com/Linux/2014-11/109214.htm Zypper是SUSE Linux中用于安装,升级,卸载,管理仓库.进行各种包查询的命令行接口. ...
- iOS7中的多任务 - Background Fetch,Silent Remote Notifications,Background Transfer Service
转自:http://onevcat.com/2013/08/ios7-background-multitask/ 在IOS 7 出来不就,公司内部也组织了一次关于IOS 7 特性的的分享,今天看见on ...
- 能在xcode5中开发基于IOS7sdk的应用程序兼容ios4.3之后的系统吗?
能在xcode5中开发基于IOS7sdk的应用程序兼容ios4.3之后的系统吗?
- Oracle 专用模式(DEDICATED) 和 共享模式(SHARE)
Oracle 是一门博大精深的技术.玩了2年的oracle,依旧还有很多知识点不清楚. 昨天群里的朋友提到了 DEDICATED 和 SHARE 两种模式. 不清楚,默默的做点功课了.从网上搜了点知识 ...
- 【linux c】setsockopt 详解
转自:http://blog.csdn.net/zhonglinzhang/article/details/9183229 功能描述: 获取或者设置与某个套接字关联的选 项.选项可能存在 ...
- Java 集合系列Stack详细介绍(源码解析)和使用示例
Stack简介 Stack是栈.它的特性是:先进后出(FILO, First In Last Out). java工具包中的Stack是继承于Vector(矢量队列)的,由于Vector是通过数组实现 ...
- Hyperledger 项目
https://github.com/hyperledger/fabric.githttps://github.com/hyperledger/blockchain-explorer.githttps ...
- Apache的三种工作模式
Web服务器Apache目前一共有三种稳定的MPM(Multi-Processing Module,多进程处理模块)模式. 它们分别是prefork,worker和event,它们同时也代表这Apac ...
- [转]redis配置文件redis.conf的详细说明
转自: http://www.sufeinet.com/thread-8047-1-1.html # Redis 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格 ...