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. iOS 引用外部静态库(.a文件)时或打包.a时,Category方法无法调用。崩溃

    我的这个是MJRefresh,学习打.a包Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ...

  2. b树的实现

    花了蛮长时间实现的b树插入操作.有时间再实现其他操作. #include <stdio.h> #include <stdlib.h> #define M 5 enum KeyS ...

  3. Python 字符串换行的几种方式

    第一种: x0 = '<?xml version="1.0"?>' \ '<ol>' \ ' <li><a href="/pyt ...

  4. Unity-SendMessage

    每一个对象都有SendMessage,BroadcastMessage,SendMessageUpwards 三个发送消息的方法! 1.功能: 执行某个对象中的某个方法!   2.实现原理 反射   ...

  5. Android 图片文字单位 px、dp、sp区别

    文章来源:http://www.cnblogs.com/bjzhanghao/archive/2012/11/06/2757300.html px:像素,一个像素点,1px代表屏幕上一个物理的像素点: ...

  6. HDU 3336 Count the string ( KMP next函数的应用 + DP )

    dp[i]代表前i个字符组成的串中所有前缀出现的次数. dp[i] = dp[next[i]] + 1; 因为next函数的含义是str[1]~str[ next[i] ]等于str[ len-nex ...

  7. 团队Alpha版本(七)冲刺

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  8. nyoj 题目36 最长公共子序列

    最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公共子序列也称作最 ...

  9. c#中获得MD5字符串方法

    在用户登录的过程中,我们会遇到要查询对比用户名密码的是否存在或者是否正确,但是数据库中存放的是通过MD5加密的字符串,所有我们可以先把用户输入的用户名或者是密码先转为DM5字符串再跟数据库查出的MD5 ...

  10. P1242 新汉诺塔

    题目描述 设有n个大小不等的中空圆盘,按从小到大的顺序从1到n编号.将这n个圆盘任意的迭套在三根立柱上,立柱的编号分别为A.B.C,这个状态称为初始状态. 现在要求找到一种步数最少的移动方案,使得从初 ...