PAT1049:Counting Ones
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的个数和。
找规律题,计算每一位对应的1的个数,然后相加,每一位的1的计算情况分三种情况:
1.如果当前位数字为0,那么该位的1的个数由更高位的数字确定。比如2120,个位为1的个数为212 * 1 = 212(个位的单位为1)。
2.如果当前位数字为1,那么该位的1的个数不但由高位决定,还由低位数字决定。比如2120百位为1,那么百位数字1的个数为2 * 100 + 20 + 1 = 221个(百位的单位为100)。
3.如果当前位数字大于1,那么该位数字1的个数为(高位数+ 1) * 位数单位。比如2120十位为2,那么十位数字1的个数为(21 + 1) * 10 = 220个(十位的单位为10)
4.继续按照上文,2120千位为2,那么千位为1的个数为(0 + 1)*1000 = 1000
5.综上2120以内的所有数字中出现1的个数为1653个。- 代码
- #include<iostream>
- using namespace std;
- int CountOnes(int n)
- {
- int factor = 1,lownum = 0,highnum = 0,cur = 0,countones = 0;
- while(n/factor)
- {
- highnum = n/(factor*10);
- lownum = n - (n/factor)*factor;
- int cur = (n/factor) % 10;
- if(cur == 0)
- {
- countones += highnum * factor;
- }
- else if (cur == 1)
- {
- countones += highnum * factor + lownum + 1;
- }
- else
- {
- countones += ( highnum + 1) * factor;
- }
- factor *= 10;
- }
- return countones;
- }
- int main()
- {
- int n;
- while(cin >> n)
- {
- cout << CountOnes(n);
- }
- }
PAT1049:Counting Ones的更多相关文章
- pat1049. Counting Ones (30)
1049. Counting Ones (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task ...
- 萌新笔记——Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))
在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- ZOJ3944 People Counting ZOJ3939 The Lucky Week (模拟)
ZOJ3944 People Counting ZOJ3939 The Lucky Week 1.PeopleConting 题意:照片上有很多个人,用矩阵里的字符表示.一个人如下: .O. /|\ ...
- find out the neighbouring max D_value by counting sort in stack
#include <stdio.h> #include <malloc.h> #define MAX_STACK 10 ; // define the node of stac ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 6.Counting Point Mutations
Problem Figure 2. The Hamming distance between these two strings is 7. Mismatched symbols are colore ...
- 1.Counting DNA Nucleotides
Problem A string is simply an ordered collection of symbols selected from some alphabet and formed i ...
- uva 11401 Triangle Counting
// uva 11401 Triangle Counting // // 题目大意: // // 求n范围内,任意选三个不同的数,能组成三角形的个数 // // 解题方法: // // 我们设三角巷的 ...
随机推荐
- Customer Form Issue: Automatic Matching Rule Set Defaults Value AutoRuleSet-1
In this Document Symptoms Changes Cause Solution References APPLIES TO: Oracle Receivables ...
- 【一天一道LeetCode】#9. Palindrome Number
一天一道LeetCode系列 (一)题目 Determine whether an integer is a palindrome. Do this without extra space. Some ...
- RedHat系列软件管理(第二版) --二进制软件包管理
RedHat系列软件管理 --二进制软件包管理 Linux学习思想-Linux相对与Windows来非常透明,因此,无论是系统,还是软件,都会有本身自带,或者是Man给提供的非常详细的说明/帮助文档, ...
- C语言实现快速翻转数组的顺序
#include <stdio.h> void Reverse(int *p , int size) { int i , tmp; for(i = 0 ; i < size/2 ; ...
- 【freeradius3】安装和拓展需求
这次主要来看看 freeradius的安装,以及Python拓展的例子,还有计费字段根据厂家进行拓展. 3.0版本的安装 参考文章 yum install libtalloc-devel wget - ...
- "《算法导论》之‘字符串’":字符串匹配
本文主要叙述用于字符串匹配的KMP算法. 阮一峰的博文“字符串匹配的KMP算法"将该算法讲述得非常形象,可参考之. 字符串‘部分匹配值’计算 KMP算法重要的一步在于部分匹配值的计算.模仿& ...
- 50行代码实现的一个最简单的基于 DirectShow 的视频播放器
本文介绍一个最简单的基于 DirectShow 的视频播放器.该播放器对于初学者来说是十分有用的,它包含了使用 DirectShow 播放视频所有必备的函数. 直接贴上代码,具体代码的含义都写在注释中 ...
- Linux - mail
使用者邮件信箱: mail 使用 wall, write 毕竟要等到使用者在在线才能够进行,有没有其他方式来联络啊? 不是说每个 Linux 主机上面的用户都具有一个 mailbox 吗? 我们可否寄 ...
- myBatis源码之XMLConfigBuilder
XMLConfigBuilder是对mybatis的配置文件进行解析的类,会对myabtis解析后的信息存放在Configuration对象中,Configuration对象会贯穿整个mybatis的 ...
- Xcode使用心得01:断点中断问题和调整编译目标
在obj-c系列博文里,我们粗浅的介绍了obj-c的一些语法以及F库中的一些标准类的使用,但是实际编写拿得出手的APP还是得老老实实在os x上用Xcode写啊!最近上网无意中发现还有支持os x和i ...