1049. Counting Ones (30)

来源:互联网 发布:js 删除预览图片 编辑:程序博客网 时间:2024/06/08 04:02

1049. Counting Ones (30)

时间限制
10 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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


#include<iostream>#include<string>using namespace std;int main(){long long N;scanf("%lld",&N);long long temp=0,count=0,level=1;while(N>0){if(N%10!=0&&N%10!=1)count+=(N/10+1)*level;else if(N%10==1){count+=(temp+1)+(N/10)*level;}else if(N%10==0){count+=(N/10)*level;}temp+=N%10*level;level*=10;N/=10;}cout<<count;}
感想:1.一位一位数字讨论,从最低位开始  2. 分类讨论 数字0或者1或者其他 3.测试点关键数字 1,11,111,110
0 0
原创粉丝点击