首页 技术 正文
技术 2022年11月15日
0 收藏 387 点赞 2,095 浏览 854 个字

参考前人的统计思想:分别统计个、十、百、、、亿等第N位上1出现的次数。

如ABCDE,在统计D位1出现的次数时,用D做分割符,ABC为Before,E为After。

分情况考虑:(n为D的length-1)

当D = 0 时,count = Before * 10^n ;

当D = 1 时,count = Before * 10^n + After;

当D > 1 时,count = (Before + 1)*10^n;

例如:

  19X8

统计X上1的次数:

1)X = 0 ,即1908 X为1的数有001x~181x,x取0~9则19为Before,8为After

此时count = 19 * 10^1 ;

2)X = 1 ,即1918 X为1的数有001x~181x,x取0~9;另外,1910~1918,则19为Before,8为After

此时count = 19 * 10^1 + (8 + 1);

3)X > 1 ,如1928 X为1的数有001x~191x,x取0~9则19为Before,8为After

此时count = (19 + 1) * 10^1 ;

特别当X在最左端时Before 为 0,最右端时After 为0

#include <stdio.h>int Count1(int n)
{
int count = ,//1出现总次数
bitCount = ,//某位1出现次数
base = ,//基数
before = n,after = , //从最右开始,则Before = n,After = 0
bitN = ;//第N位数
while(before)//向左移,还有数时循环
{
after = n % base;
before = n / (base * );
bitN = (n / base) % ;
if(bitN > )
{
bitCount = (before + ) * base;
}
else if(bitN == )
{
bitCount = (before) * base;
}
else
{
bitCount = (before) * base + (after + );
}
base *= ;
count += bitCount;
}
return count;
}int main() {
int n = ;
printf("%d\n",Count1(n));
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,367
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,858