A1049. Counting Ones (30)
2015-08-23 15:33
176 查看
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<cstdio> int main(){ int n,a = 1,sum = 0; scanf("%d",&n); int now,left,right; //当前位数,左边数字,右边数字 while(n/a){ left = n/(a*10); now = n/a%10; right = n%a; if(now==0){ sum += left*a; } else if(now==1){ sum += left*a+right+1; } else sum += (left+1)*a; a *= 10; } printf("%d\n",sum); return 0; }
相关文章推荐
- Android属于查询执行情况的电话号码
- mysql学习笔记(一):mysql的安装配置
- Oracle RAC 11g 修改归档模式
- 大数据的商业应用
- mysql 解压版方法
- 《深入浅出MySQL--数据库开发、优化与管理维护》读书笔记--开发篇
- Dijkstra算法,求单源最短路径(包含路径)
- c++中静态成员变量要在类外部再定义,否则产生link2001错误
- 10到10000中的0的个数
- 三周工作日志
- U-Boot 第二阶段代码分析
- oracle迁移表空间数据文件
- 23种设计模式全解析
- 杂谈: 房产信息泄露谁是内鬼?
- iOS UI基础-4.1应用程序管理 字典转Model
- Java动态代理
- oracle迁移表空间数据文件
- django+apache部署
- 简单纯js实现点击切换TAB标签实例
- ueditor1-4-3 编辑器使用教程