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 (≤230).
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;
}