从1到n整数中1出现的次数
2016-05-09 20:36
537 查看
#include<stdio.h> long long int Count(long long int n){ //1的个数 long long int count = 0; //当前位 long long int Factor = 1; //低位数字 long long int LowerNum = 0; //当前位数字 long long int CurrNum = 0; //高位数字 long long int HigherNum = 0; if(n <= 0){ return 0; } while(n / Factor != 0){ //低位数字 LowerNum = n - (n / Factor) * Factor; //当前位数字 CurrNum = (n / Factor) % 10; //高位数字 HigherNum = n / (Factor * 10); //如果为0,出现1的次数由高位决定 if(CurrNum == 0){ //等于高位数字 * 当前位数 count += HigherNum * Factor; } //如果为1,出现1的次数由高位和低位决定 else if(CurrNum == 1){ //高位数字 * 当前位数 + 低位数字 + 1 count += HigherNum * Factor + LowerNum + 1; } //如果大于1,出现1的次数由高位决定 else{ //(高位数字+1)* 当前位数 count += (HigherNum + 1) * Factor; } //前移一位 Factor *= 10; } return count; } int main(){ long long int a; while(scanf("%lld",&a) != EOF){ printf("%lld\n",Count(a)); } return 0; }
/article/1424419.html
相关文章推荐
- 常见的服务器性能指标
- leetcode 82-- Remove Duplicates from Sorted List II 链表 双指针 删除元素
- Sqlite基础
- MySQL创建用户与授权方法
- DOM中的三种类型的节点:元素节点,属性节点,文本节点
- SpringIOC容器-创建对象
- php页面跳转的几种方式
- IO流----文件字节流、文件字符流
- JAVA实现过滤掉文本中的表情
- swift 字典和数组
- 【BZOJ4378】[POI2015]Logistyka【树状数组】【结论题】
- CentOS 6.5环境下使用HAProxy+apache实现web服务的动静分离
- 二十二、外观设计模式
- PCA的数学原理
- 抓妖怪项目(踩坑)总结
- 检测录音程序是否被禁用 录音权限
- 第10、11周-项目3(1)
- leetcode.153. Find Minimum in Rotated Sorted Array
- struts2页面属性值回显
- Python学习笔记(二)列表统计词频