http://noi.openjudge.cn/ch0105/40/

40:数1的个数-拓展变形

总时间限制: 
1000ms

内存限制: 
65536kB
描述

给定一个十进制正整数n,写下从1到n的所有整数,然后数一下其中出现的数字“1”的个数。

例如当n=2时,写下1,2。这样只出现了1个“1”;当n=12时,写下1,2,3,4,5,6,7,8,9,10,11,12。这样出现了5个“1”。

输入
正整数n。1 <= n <= 10000。
输出
一个正整数,即“1”的个数。
样例输入
12
样例输出
5

#include <iostream>
#include <cstdio>
#include <cstring> //for memset
#include <algorithm> //for max
using namespace std; int cntone(int n,int x)
{
int cnt=,i=,tmp;
for(i=;i<=n;i++)//1~n
{
tmp=i;
while(tmp)
{
if(tmp%==x)
cnt++;
tmp/=;
}
}
return cnt;
}
int main()
{
int n,x;
while(cin>>n>>x)
{
cout<<cntone(n,x)<<endl;
}
}

time:nlog(n) 计算数字 x 在 1-n 中出现的次数

找规律:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
/*只求1的个数*/
int cntone(int n)
{
int cnt=;
for(int i=;i<=n;i*=)//1~n
{
int a=n/i,b=n%i;
cnt+=(a+)/*i+((a%)==?b+:); //之所以补8,是因为当百位为0,则a/10==(a+8)/10,当百位>=2,补8会产生进位位,效果等同于(a/10+1)
}
return cnt;
}
int main()
{
int n;
while(cin>>n)
{
cout<<cntone(n)<<endl;
}
}

time:logn 计算数字 1 在 1-n 中出现的次数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
// 计算数字 X (不包括0)在 1-n 中出现的次数。
// X = 0 时,规律与上面给出的规律不同,需要另行考虑。
int cntone(int n,int x)
{
int cnt=,tmp;
for(int i=; tmp=n/i; i*=)//1~n
{
cnt += (tmp/)*i;
int cur = tmp%;
if(cur>x)
{
cnt+=i;
}
else if(cur == x)
{
cnt+=n-tmp*i+;
}
}
return cnt;
}
// X = 0 时
int cntzero(int n)
{
int cnt=,k;
for(int i=; (k=n/i)/; i*=)
{
cnt+= (k/)*i;
if(k%==)
{
cnt+=n-k*i+-i;
}
}
return cnt;
}
//合并:计算数字 X 在 1-n 中出现的次数,对 X 从 0 到 9 都有效:
int cnts(int n,int x)
{
int cnt=,k;
for(int i=; k=n/i; i*=)
{
int high=k/;
if(x==)
{
if(high) high--;
else break;
}
cnt+=high*i;
int cur=k%;
if(cur>x) cnt+=i;
else if(cur==x) cnt+=n-k*i+;
}
return cnt;
}
int main()
{
int n,x;
while(cin>>n>>x)
{
cout<<cntone(n,x)<<" "<<cntzero(n)<<" "<<cnts(n,x)<<endl;
}
}

计算数字 X 在 1-n 中出现的次数,对 X 从 0 到 9 都有效

2. 输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int cnts(int x)
{
int cnt=;
while(x)
{
if(x%==) cnt++; //n&1
x/=; //n>>=1;
}
return cnt;
} int main()
{
int n;
while(~scanf("%d",&n),n)
{
int cnt=cnts(n);
while(n++)
{
if(cnt==cnts(n))
{
cout<<n<<endl;
break;
}
}
}
}

poj 2453

 51nod 1042

数字0-9的数量

模板:统计1~n内x的个数的更多相关文章

  1. ls命名 | Linux统计文件夹内的文件个数

    ls命名 man ls -R 递归列出全部的目录内容 recusive -a 列出所有的文件(包括以 . 开头的隐藏文件) all -r 逆序排列 reverse -t 按照时间信息排序 time - ...

  2. JAVA统计一定范围内的质数个数

    public class TestNumber{ public static void main(String[] args){ System.out.println(roundPrimeCount( ...

  3. 【模板小程序】求M~N范围内的质数个数

    /* 本程序说明: [编程题] 求素数 时间限制:2秒 空间限制:32768K 输入M.N,1 < M < N < 1000000,求区间[M,N]内的所有素数的个数.素数定义:除了 ...

  4. bash 统计在线时长最长的十个玩/统计一天内一直处于不活跃状态的玩家的百分比

    1.某游戏的客户端每隔5分钟会向服务端报告一次玩家的账户积分,如果两次报告的时间间隔不大于5分钟,认为该玩家在这5分钟内在线,假设报告数据的格式如下: IP                   Dat ...

  5. L - Prime Number(求n内质数的个数)

    Description Write a program which reads an integer n and prints the number of prime numbers which ar ...

  6. HDU-1695 GCD(求一个区间内与一个数互质的个数)

    题意: 给你一个T,是样例的个数,接下来是五个数l1,r1,l2,r2,k  前四个数代表两个区间(l1,r1),(l2,r2)这个题l1=1,l2=1; 取x1属于(1,r1),x2属于(1,r2) ...

  7. Tocmat 统计tomcat进程内的线程数

    获取tomcat进程pid ps -ef | grep tomcat 统计该tomcat进程内的线程个数 ps -Lf  558899 | wc -l

  8. hdu 4630 查询[L,R]区间内任意两个数的最大公约数

    No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

随机推荐

  1. 《Cracking the Coding Interview》——第7章:数学和概率论——题目5

    2014-03-20 02:20 题目:给定二维平面上两个正方形,用一条直线将俩方块划分成面积相等的两部分. 解法:穿过对称中心的线会将面积等分,所以连接两个中心即可.如果两个中心恰好重合,那么任意穿 ...

  2. 嵌入式(Embedded System)笔记 —— Cortex-M3 Introduction and Basics(下)

    随着课内的学习,我想把每节课所学记录下来,以作查阅.以饲读者.由于我所上的是英文班课程,因此我将把关键术语的英文给出,甚至有些内容直接使用英文. 本次所介绍内容仍是关于Cortex-M3的基础内容,相 ...

  3. python 学习分享-实战篇类 Fabric 主机管理程序开发

    # 类 Fabric 主机管理程序开发: # 1. 运行程序列出主机组或者主机列表 # 2. 选择指定主机或主机组 # 3. 选择让主机或者主机组执行命令或者向其传输文件(上传/下载) # 4. 充分 ...

  4. dpkg.cfg

  5. [常识]Windows系统里休眠和睡眠的区别?

    睡眠和休眠都是笔记本电脑的节能方式,但有细微的差别: 睡眠还保持着开机状态的,休眠是关机了,但是再次开机之后和关闭时的系统状态是一样的. 睡眠还是保持着系统运行数据在内存中,而休眠则将内存中的数据保存 ...

  6. Nginx和Squid配合搭建的Web服务器前端系统

    这个架构是目前我个人觉得比较稳妥并且最方便的架构,易于多数人接受: 前端的lvs和squid,按照安装方法,把epoll打开,配置文件照搬,基本上问题不多. 这个架构和app_squid架构的区别,也 ...

  7. hdu 2544 最短路 (最短路径)

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. 内存检测工具valgrind

    valgrind --tool=memcheck --leak-check=full --error-limit=no  --trace-children=yes  ./server valgrind ...

  9. [codeforces] 449C Jzzhu and Apples

    原题 质因数分解后贪心即可(最后贪2) #include<cstdio> #include<vector> #include<stack> #include< ...

  10. DHTMLX 表格组件(dhtmlxGrid )使用介绍

    String sql = select {s.*} from t_student s where s.age22; SQLQuery slqQuery = session.createSQLQuery ...