1049 Counting Ones

1049 Counting Ones (30 分)

The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12.

Input Specification:

Each input file contains one test case which gives the positive N (≤2​30​​).

Output Specification:

For each test case, print the number of 1's in one line.

Sample Input:

12

Sample Output:

5

题意分析:

(1)给出一正整数N,求从1到N之间的所有数当中含有1的个数

(2)此题最容易想到的是从1到N遍历,求每个数中包含1的个数,但这样在数字比较大时,肯定超时。如果不遍历,那要怎么去求呢?这就需要寻找数学规律了。我们先从每一位开始:求当某个位置为1时共有有多少个数.以百为为例,如12145、12045、12145.即可能存在三种情况:(我们求的是某位为1的情况,如百位100~199,就不考虑101了,因为在个位和十位就已经求过了,有叠加,这点不好理解,仔细想想

①若此数百位本身就等于1,如12145,则大于等于100的有100~199、1100~1199、2100~2199...、11100~11199,另外还有12100~12145;所以当百位为1时,分高位部分和低位部分,即百位为1的数有12*100+45+1个;

②若百位等于0,如12045,则大于等于100的有100~199、1100~1199、2100~2199...、11100~11199,即百位为1的数有12*100个

③若百位大于1,则12345,则大于等于100的有100~199、1100~1199、2100~2199...、11100~11199、12100、12199,即百位为1的数有(12+1)*100个

(3)然后按照此思路同样去求其他位置为1的个数

#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#include<cstdio>
#include<string>
#include<bitset>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;

#define e exp(1)
#define pp acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b) {
	return b?gcd(b,a%b):a;
}

int f(int n)
{
	int i=1,index,hight,low,sum=0;
	while(n/i!=0)
	{
		index=(n/i)%10;
		hight=n/(i*10);
		low=n-(n/i)*i;
		if(index==0)sum+=hight*i;
		else if(index==1)sum+=hight*i+low+1;
		else if(index>1)sum+=(hight+1)*i;
		i*=10;
	}
	return sum;
}
int main()
{
	int n;scanf("%d",&n);
	printf("%d\n",f(n));
	return 0;
}

 

题目简述: 给定一个整数n,你需要求出1到n这n个整数的二进制(binary)表示中数字1出现的次数。例如,对于n=3,1到3这3个整数的二进制表示分别是:1, 10, 11,其中1出现的次数为2次。因此,输出2即可。 题目思路: 这道题可以用暴力求解法:枚举1到n的每个整数,然后统计对应的二进制表示中1的个数即可。但此种做法不适用于大数据。因此,我们需要寻找更为高效的算法。其中一种常见方法是用每一位的数字来计算。 例如,对于一个二进制数11010,我们可以从低到高,第一位统计在0~1范围内出现的次数,第二位统计在0~3范围内出现的次数,第三位在0~7范围内,第四位在0~15范围内,最后统计总体中的1的个数。因为,我们可以发现,对于n的第i位上的数字,它出现1的次数由(n / (i*10) * i)个1,除去i-1位之后,如果大于2,则还要加上i个1。再来看一个具体的例子,对于11010,第一位上有10种情况,0出现5次,1也出现5次,对于第二位,共有110, 101, 100, 011, 010, 001, 000七种情况,其中从00到01这两种情况中,每个数字都出现了两次1,因此一共出现了2*2=4次1。同样地,对于 101 和 011 这两种情况,也是如此。 因此,最终代码如下(基于C++): #include <iostream> using namespace std; #define int long long//避免溢出 signed main(){ int n, ans; cin >> n; for(int l = 1, r; l <= n; l = r + 1){ int k = n / l; r = n / k; ans += (r - l + 1) * (k / 10); int t = (k % 10) * l; if(t >= l) ans += t - l + 1; } cout << ans << endl; return 0; } 注意:本题的数据范围比较大,因此需要使用int long long类型避免溢出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值