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

提交代码

思路:

统计每位的1的贡献。

对于k位(k>=1):

1.Ak=0,count+=AnAn-1....Ak+1AkAk-1....A1*10^(k-1)

2.Ak=1,count+=AnAn-1....Ak+1AkAk-1....A1*10^(k-1)+Ak-1Ak-2...A1+1

3.Ak>=2,count+=(AnAn-1....Ak+1AkAk-1....A1+1)*10^(k-1)

 #include<cstdio>
#include<stack>
#include<cstring>
#include<iostream>
#include<stack>
#include<set>
#include<map>
using namespace std;
//count的最大值是1036019223
int main(){
int n;
scanf("%d",&n);
long long base=;
long long count=;
int frpart,afpart,a;
while(n>=base){
a=n/base%;
frpart=n/(*base);
afpart=n%base;
count+=frpart*base;
if(a==){
count+=afpart+;
}
else if(a>){
count+=base;
}
base*=;
}
printf("%lld\n",count);
return ;
}

最新文章

  1. 怎么可以让div自适应屏幕的高度?(已解决)
  2. 装饰模式(Decorate Pattern)
  3. Jade之Case
  4. MyBatis知多少(10)应用程序数据库
  5. JAVA基础知识之JVM-——URLClassLoader
  6. CF 486D vailid set 树形DP
  7. Android中解析JSON形式的数据
  8. iOS开发--绘图教程
  9. NOI题库7624 山区建小学(162:Post Office / IOI2000 POST OFFICE [input] )
  10. Android的线程和线程池
  11. Window2003、xp远程备份数据库文件(xcopy+rar+pscp)
  12. App 组件化/模块化之路——Repository 模式
  13. 玩转Ecs服务器之搭建Ftp
  14. 【原】Java学习笔记025 - 内部类
  15. 为什么使用Nosql:Nosql和SQL的区别
  16. ansible笔记(7):常用模块之系统类模块
  17. laravel 模型事件 updated 触发条件
  18. 二叉树转换成森林&amp;amp;森林变成二叉树
  19. main方法之args参数
  20. Shrio第一天——入门与基本概述

热门文章

  1. Velocity的layout功能
  2. System.getProperty()获取系统的相关属性
  3. Python-Redis的Hash操作
  4. [hdu4734]F(x)数位dp
  5. htmlparser API
  6. HTML5+JavaScript动画基础 完整版 中文pdf扫描版
  7. 引用静态资源的url添加版本号,解决版本发布后的浏览器缓存有关问题
  8. ubuntu下php7安装及配置
  9. 【hibernate-笔记】
  10. 在Android中使用FlatBuffers(上篇)