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范围内,任意选三个不同的数,能组成三角形的个数 // // 解题方法: // // 我们设三角巷的 ...
随机推荐
- Java 反射之JDK动态代理
Proxy提供用于创建动态代理类和代理对象的静态方法,它也是所有动态代理类的父类.如果我们在程序中为一个或多个接口动态地生成实现类,就可以使用Proxy来创建动态代理类:如果需要为一个或多个接口动态的 ...
- 11_Eclipse中演示Git版本的创建,历史版本的修改,创建分支,合并历史版本和当前版本
1 执行以下案例: 某研发团队2011年初开发了一款名为Apollo的信息系统,目前已发布v1.0版本.此项目初期已有部分基础代码, 研发团队再此基础代码上经过3个月的努力发布了一个功能相对完备 ...
- 【一天一道LeetCode】#23. Merge k Sorted Lists
一天一道LeetCode系列 (一)题目 Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- STL算法设计理念 - 函数适配器
1)函数适配器的理论知识 2)常用函数函数适配器 标准库提供一组函数适配器,用来特殊化或者扩展一元和二元函数对象.常用适配器是: 1.绑定器(binder): binder通过把二元函数对象的一个实参 ...
- Process Order API - How To Scripts
In this Document Purpose Questions and Answers References APPLIES TO: Oracle Order Management ...
- android ViewPager+Fragment之懒加载
说说写这篇博客的背景吧,前两天去面试,问到一个问题说的是:比如我们首页,是有3个fragment构成的,并且要是实现作用可以滑,那么这个最好的选择就是ViewPager+fragment了,但是我们知 ...
- 【Java编程】Java基本数据类型
在较前面的一篇博文<C/C++基本数据类型>中,我主要介绍了c/c++的基本数据类型.我们知道C语言没有具体规定各类数据类型所占内存的字节数,只要求long型数据长度不小于int型,sho ...
- ActiveX数据对象之事务控制在VB和DELPHI中的应用
本文发表在中国人民解放军"信息工程大学"学报 2001年第3期. ActiveX数据对象之事务控制在VB和DELPHI中的应用 ...
- Linux文件系统构成(第二版)
Linux文件系统构成 /boot目录: 内核文件.系统自举程序文件保存位置,存放了系统当前的内核[一般128M即可] 如:引导文件grub的配置文件等 /etc目录: 系统常用的配置文件,所以备份系 ...
- 销售订单-修改量-高级定价关联sql
修改量消耗明细 --修改量消耗明细 SELECT t.name, t.comments, t.version_no, cux_rebate_pub.get_hou_name(p_organizatio ...