1049. Counting Ones (30)

时间限制
100 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的个数和。
找规律题,计算每一位对应的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的更多相关文章

  1. pat1049. Counting Ones (30)

    1049. Counting Ones (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task ...

  2. 萌新笔记——Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))

    在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...

  3. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  4. ZOJ3944 People Counting ZOJ3939 The Lucky Week (模拟)

    ZOJ3944 People Counting ZOJ3939 The Lucky Week 1.PeopleConting 题意:照片上有很多个人,用矩阵里的字符表示.一个人如下: .O. /|\ ...

  5. 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 ...

  6. 1004. Counting Leaves (30)

    1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  7. 6.Counting Point Mutations

    Problem Figure 2. The Hamming distance between these two strings is 7. Mismatched symbols are colore ...

  8. 1.Counting DNA Nucleotides

    Problem A string is simply an ordered collection of symbols selected from some alphabet and formed i ...

  9. uva 11401 Triangle Counting

    // uva 11401 Triangle Counting // // 题目大意: // // 求n范围内,任意选三个不同的数,能组成三角形的个数 // // 解题方法: // // 我们设三角巷的 ...

随机推荐

  1. 【Android 应用开发】Android 数据存储 之 SQLite数据库详解

    . 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19028665 . SQLiteDataBase示例程序下 ...

  2. 理解WebKit和Chromium: Chromium网络栈

    转载请注明原文地址:http://blog.csdn.net/milado_nju ## 概述 前面讲到Chromium的资源加载机制,在调用栈上,提到URLRequest之后就戛然而止,在这之下就是 ...

  3. mysql进阶(十二)常见错误汇总

    原因:外键名不能重复

  4. gcc学习(二)[第二版]

    头文件与库文件 1.概要 在使用C语言和其他语言进行程序设计的时候,我们需要头文件来提供对常数的定义和对系统及库函数调用的声明. 库文件是一些预先编译好的函数集合,那些函数都是按照可重用原则编写的.它 ...

  5. 一个 developer 的进化

    作为一名开发者已十年,回顾过往大概经历了这么几个阶段,如下图所示: Develop Code 作为刚走出学校的学生进入公司,在最初的 1-2 年内就处于该阶段. 不停的开发代码,为系统的大厦添砖加瓦, ...

  6. CCRenderBuffer初始化中的render state参数

    绘制纹理三角形的渲染状态(render state)已经被CCSprite基类设置过了,所以你可以简单的将self.renderState传递过去就可以了. 渲染状态是混合模式(blend mode) ...

  7. LIRe 源代码分析 6:检索(ImageSearcher)[以颜色布局为例]

    ===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...

  8. Linux常用命令(第二版) --网络通信命令

    网络通信命令 1.write /usr/bin/write 格式: write [用户名] #用于向用户发送信息,前提是这个用户已经登录到了这台服务器主机,不然的话,也没有办法给他留言,所以,writ ...

  9. 网站开发进阶(十一)如何将一个jsp页面嵌套在另一个页面中

    如何将一个jsp页面嵌套在另一个页面中 这个在做网页中常要用到,有些通用的内容可集中放在一个页面文件中,其它要用到这些内容的页面只需要包含(引用)这个通用文件即可.这样便于维护,如果有很多网页,当通用 ...

  10. 自动生成材质Material(Unity3D开发之十九)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46854411 ...